mysql三表联合查询问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql三表联合查询问题相关的知识,希望对你有一定的参考价值。

以上是表结构。
member表中student_id和student表的row_id关联,student表中的city_id和region表中的region_id关联。
现在要取到member表中的image,student表中的name,region表中的region_name,该如何写sql。我这么写没有得到正确的结果:select m.row_id,m.image,s.account,s.name,s.store,r.region_name from memeber m left join student s on m.student_id=s.row_id left join region r on s.city_id=r.region_id

参考技术A select 
    a.row_id
    a.image,
    b.name,
    c.region_name
from
    member a,
    join student b,region c on (a.student_id =b.row_id and b.city_id = c.region_id );

参考技术B 那你要什么样的结果?举个结果的例子?追问

例如:

追答你自己的sql语句就可以啊,

select m.row_id,m.image,s.account,s.name,r.region_name 
from memeber m inner join student s on m.student_id=s.row_id 
    inner join region r on s.city_id=r.region_id

还是,你要求1个人最多只能有一条数据返回 ???

本回答被提问者采纳

mysql 三表联合查询

A表 id cid title
B表 id cid title
(AB表的结构一样)
C表 cid cname
C表示AB两个的分类表,AB两个都使用的C表的分类,问题来了
查询cid=1的内容?
select * from a where cid=1 和select * from b where cid=1 这两sql可以组合一起吗?

使用UNION联合两个语句即可:
select * from a where cid=1
UNION
select * from b where cid=1
参考技术A select * from a as a, b as b where a.cid=b.cid and a.cid=1
是这样吗?追问

不行的,它会把A表和B表的字段都一起列出来作为一条记录
查询结果是
第1条记录:id cid title id cid title
第2条记录:id cid title id cid title
....

追答

2个Sql组合 就用UNION
select * from a where cid=1
UNION
select * from b where cid=1

以上是关于mysql三表联合查询问题的主要内容,如果未能解决你的问题,请参考以下文章

mysql三表联合查询

MySQL三表查询

求三表联合查询的SQL查询语句

求三表联合查询的SQL查询语句

三表联合查询出错是啥原因?

sql联合查询UNION问题