日期:2014-05-16  浏览次数:21079 次

3表如何联合查询,在线等
表1
hostname
111

表2
hostname
222
111
333
444

表3
hostname
555
666
777
888

表1是条件,要在表2或者表3中查询出hostname名相同的,谁能告诉我啊,谢谢拉

------解决方案--------------------
查出表2和表三中和表1相同的

select * from table1,table2 where table1.hostname=table2.hostname union all
select * from table1,table3 where table1.hostname=table3.hostname

------解决方案--------------------
错在 select * ,应该指明是那个表的。

select db2.table2.* from db1.table1,db2.table2 where db1.table1.hostname=db2.table2.hostname union all
select db2.table2.* from db1.table1,db2.table3 where db1.table1.hostname=db2.table3.hostname