2012-01-05 143 views
0

我遇到了MySQL連接查詢的問題 - 不幸的是我不太擅長連接。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.display_order

顯示來自page_section_sub行有序目前我有這樣的:

$query="SELECT * FROM page_section_sub WHERE page_section_id IN (1,2,3,4,5) 
ORDER BY display_order"; 

但這不是因爲缺少連接而工作。我該怎麼做呢?

+0

類似的東西:'選擇PSS *從page_section_sub PSS上pss.page_section_id = ps.id加入page_section PS其中ps.id在ps.display_order'的1到5之間 – greut 2012-01-05 17:37:28

回答

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 
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; 
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