在线求助 多表SQL查询语句
我在一张表A case_ask_revert,字段如下:
revert_id
user_account
revert_date
revert_remark
score
还有两张用户表lawyer_user B 和unit_user C ,其表结构人发别如下:
表lawyer_user
user_id
user_account
name
tel
address
表unit_user
user_id
user_account
name
tel
address
现在要查出表A中的信息同时查出与表A关联的表B和表C的信息,其SQL语句如下:
select A.*,B.name,B.tel,B.address,C.name,C.tel,C.address from case_ask_revert A left join lawyer_user B on A.user_account = B.user_account left join unit_user C on A.user_account = C.user_account
如果想 name,tel,address其用一个能不能查出来,比如说像如下的:
select A.*,B.name,B.tel,B.address from case_ask_revert A left join lawyer_user B on A.user_account = B.user_account left join unit_user C on A.user_account = C.user_account
或select A.*,C.name,C.tel,C.address from case_ask_revert A left join lawyer_user B on A.user_account = B.user_account left join unit_user C on A.user_account = C.user_account
反正得到的结果就是A.*,name,tel,address,只用这几个字段就查出来。
在线求助,请高手指点!
[解决办法]
好象是這麼個意思
--如果要合併重復
select A.*,B.name,B.tel,B.address from case_ask_revert A left join lawyer_user B on A.user_account = B.user_account
Union
select A.*,C.name,C.tel,C.address from case_ask_revert A left join unit_user C on A.user_account = C.user_account
--如果不需要合併重復
select A.*,B.name,B.tel,B.address from case_ask_revert A left join lawyer_user B on A.user_account = B.user_account
Union All
select A.*,C.name,C.tel,C.address from case_ask_revert A left join unit_user C on A.user_account = C.user_account