2015-01-05 235 views
-1

我有用於顯示2個表tbl_categoryMySQL查詢和PHP代碼

id category 
1 cat1 
2 cat2 
3 cat3 

和第二表Tbl_book

id cat_id book_name 
1 1  book1 
2 1  book2 
3 2  book4 
4 1  book5 

MySQL查詢和PHP代碼

cat1 book1 book2 book5 
cat2 book4 

回答

3

怎麼樣

select c.category, 
     group_concat(b.bookname separator ' ') as books 
from tbl_category c 
inner join tbl_book b on b.cat_id = c.id 
group by c.category 
+0

感謝您的幫助,我想要5個書的限制然後 –

+0

一個方法來做到這一點將在這裏描述:http://stackoverflow.com/a/23608554/575376 –