[英]Inner join more than 2 tables in a single database
i want to innerjoin more than 3 tables in one select query in one database all of them are having rm_id as a common column but i want to display the rows of all tables in one sql query is it possible if it is then i would hearly request u to provide some code 我想在一个数据库中的一个选择查询中对3个以上的表进行内部联接,所有这些表都将rm_id作为公共列,但是我想在一个sql查询中显示所有表的行,如果可能的话,我会听到你提供一些代码
select * from
bk_det inner join
bk_rep inner join
bk_sec inner join
mut_det inner join
rm_det inner join
soil_det
on
bk_det.rm_id = bk_rep.rm_id = bk_sec.rm_id = mut_det.rm_id = rm_det.rm_id = soil_det.rm_id
You need an on clause for each inner join. 每个内部联接都需要一个on子句。 eg
例如
select * from a
inner join b on a.id = b.id
inner join c on b.id = c.id
You are missing the ON
clause for each join: 您缺少每个联接的
ON
子句:
select *
from bk_det
inner join bk_rep
on bk_det.rm_id = bk_rep.rm_id
inner join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
inner join mut_det
on bk_sec.rm_id = mut_det.rm_id
inner join rm_det
on mut_det.rm_id = rm_det.rm_id
inner join soil_det
on rm_det.rm_id = soil_det.rm_id
Note: you will have to check the join condition column names since I do not know your table structure 注意:由于我不知道您的表结构,因此您必须检查联接条件列名
If you need help with learning join syntax, here is a great visual explanation of joins . 如果您在学习连接语法方面需要帮助,这是对joins的直观说明 。
Since you are using an INNER JOIN
this will return records if the rm_id
exists in each table. 由于您使用的是
INNER JOIN
如果每个表中都存在rm_id
这将返回记录。
You might need to use a LEFT JOIN
: 您可能需要使用
LEFT JOIN
:
select *
from bk_det
left join bk_rep
on bk_det.rm_id = bk_rep.rm_id
left join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
left join mut_det
on bk_sec.rm_id = mut_det.rm_id
left join rm_det
on mut_det.rm_id = rm_det.rm_id
left join soil_det
on rm_det.rm_id = soil_det.rm_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.