请问一个SQL查多个表的语法

来源:百度知道 编辑:UC知道 时间:2024/06/19 11:42:26
表:table1,字段:id,uid
表:table2,字段:id,book
表:table3,字段:id,info
一个SQL同时查三个表请详细

楼上说的都不好,看着代码乱糟糟的,这样写就可以了

select a.id,a.uid,b.book,c.info from table1 a inner join table2 b on a.id=b.id inner join table3 c on a.id=c.id

或者:
select a.id,a.uid,b.book,c.info from table1 a,table2 b,table3 c where a.id=b.id and a.id=c.id

说明,select后不能加星号,否则出来的列id就重复了,表名后面的别名不用加as,推荐使用第一种写法,那样看着条理比较清晰

以上,希望对你有所帮助!

select a.id,a.uid,b.book,c.info from table1 a ,table2 b table3 c
where a.id=b.id and a.id=c.id
group by a.id,a.uid,b.book,c.info

方法1: select * from table1 as a,table2 as b,table3 as c where a.id=b.id and b.id=c.id
方法2: select * from table1 as a inner join table2 as b on a.id=b.id inner join table3 as c on b.id=c.id

用视图勾一下就出来了,最简单