首页 新闻 赞助 找找看

mysql3表关联查询

0
悬赏园豆:15 [已解决问题] 解决于 2017-02-08 14:39

表一:table1 字段: a , b , c , d 

表二:table2 字段: d , e , f , h 

表三:table3 字段: e , g , m , p

表三是基础表,要查询字段 a, b , f, h , m

要怎么写mysql语句啊 

看你灬梨涡浅笑的主页 看你灬梨涡浅笑 | 初学一级 | 园豆:96
提问于:2017-02-08 14:01
< >
分享
最佳答案
1
select  table1.a
       ,table1.b
       ,table2.f
       ,table2.h
       ,table3.m

from table3
left join table2
on table3.e = table2.e
left join table1
on table2.d = table1.d

如果表与表之间的条件更多的话,可以参照,至于内联还是外联就看你自己的业务需求了,上面写的是外联

收获园豆:14
truxt | 菜鸟二级 |园豆:218 | 2017-02-08 14:16

谢谢大神,我解决了,你这种写法我等下试试

看你灬梨涡浅笑 | 园豆:96 (初学一级) | 2017-02-08 14:39

select distinct t1.a,
t1.b,
t2.f,
t2.h,
t3.m
from table1 t1,
table2 t2,
table3 t3
where t1.d=t2.d
and t2.e=t3.e
这是我的解决办法,也是一位大神告诉我的。

看你灬梨涡浅笑 | 园豆:96 (初学一级) | 2017-02-08 14:40

@看你灬梨涡浅笑: 你的写法也是对的,相当于是内联,table1,table2,table3都要有匹配的数据才能检索出来,如果table1和table2中没有匹配的数据就查询不出来

truxt | 园豆:218 (菜鸟二级) | 2017-02-08 14:44
其他回答(1)
0

关联关系不够具体

收获园豆:1
让我发会呆 | 园豆:2929 (老鸟四级) | 2017-02-08 14:11

谢谢

 

支持(0) 反对(0) 看你灬梨涡浅笑 | 园豆:96 (初学一级) | 2017-02-08 14:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册