简体   繁体   中英

mysql php grouping by a category name

Quick brief: Just now I currently get a list of results by using the below code. I am trying to group my u_feed results by c_feed id 's.

$uid = '15';

SELECT u.uid, u.fid, f.url,f.icon, u.title, f.favicon, f.domain FROM u_feed u INNER JOIN feed f ON f.id = u.fid WHERE u.uid = '$uid'

I have just added a table called c_feed which will hold the name and uid of each category. The u_feed table will then use the column cid to check c_feed for the id .

table c_feed

在此处输入图片说明

table feed 在此处输入图片说明

table u_feed 在此处输入图片说明

I tried SELECT u.uid, u.fid, f.url, f.icon, u.title, f.favicon, f.domain, c.cname FROM u_feed u INNER JOIN feed f ON f.id = u.fid INNER JOIN c_feed c ON c.id = u.cid WHERE u.uid = '15' LIMIT 0 , 30

This only returned 在此处输入图片说明

But it should have 11 rows (image below without grouping)

在此处输入图片说明

It is your inner join which only include the record if there is an associated record in c_feed. So the only records you got back were the two WITH a cid .

You might be looking for left join s.

SELECT u.uid, u.fid, f.url, f.icon, u.title, f.favicon, f.domain, c.cname 
FROM u_feed u 
LEFT JOIN feed f ON f.id = u.fid 
LEFT JOIN c_feed c ON c.id = u.cid 
WHERE u.uid = '15' LIMIT 0 , 30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM