我遇到了MySQL连接查询的问题 - 不幸的是我对连接不太好。
Table page_section:
-------------------
id
page_id
name
display_order
Table page_section_sub:
-----------------------
id
page_section_id
page_id
content
一个page_section
可以有多个page_section_sub
在我的页面上,我想显示page_section_sub
page_section.display_order
行
目前我有这个:
$query="SELECT * FROM page_section_sub WHERE page_section_id IN (1,2,3,4,5)
ORDER BY display_order";
但这不起作用,因为它缺少连接。我该怎么做?
答案 0 :(得分:2)
select pss.*
from page_section_sub pss
inner join page_section ps on pss.page_section_id = ps.id
where pss.page_section_id in (1, 2, 3, 4, 5)
order by ps.display_order
答案 1 :(得分:0)
尝试这种方式:
SELECT * FROM page_section
LEFT JOIN page_section_sub ON page_section.page_id = page_section_sub.page_id
WHERE page_section_id IN (1,2,3,4,5)
ORDER BY display_order;
答案 2 :(得分:0)
试试这个:
Select page_section_id.* from page_section_id natural join page_section
WHERE page_section_id IN (1,2,3,4,5) order by page_section_id.display_order