[英]Mysql Join:get total favourites for each item along with each row
Sample input for my DB
我的DB
样本输入
1)cushbu_users 1)cushbu_users
id first_name last_name email
1 sh s sh@sh.com
2 sb s sh1@sh.com
2)cushbu_art 2)cushbu_art
id user_id title image_name
1 1 cool cool.jpeg
2 2 funny funny.jpeg
3 1 blaaa blaa.jpeg
4 2 foo foo.jpeg
3)cushbu_mark_user_favorites-Store the details of favourited item 3)cushbu_mark_user_favorites-存储收藏项的详细信息
id user_id art_id
1 1 1
2 1 2
3 2 1
4 2 2
As you see two users Favorited two arts so the total count
for favourite of each art is `two`
I want to get the favourited arts of each user with total favourite 我想获得每个用户最喜欢的艺术,并且总喜欢
My excepted output for user of id=1 我的id = 1用户的例外输出
art_id artist_name total_fav
1 sh s 2
2 sb s 2
Here is the query for that 这是该查询
SELECT
cushbu_art.id AS art_id,
cushbu_art.title,
cushbu_art.image_name,
CONCAT(
cushbu_users.first_name,
' ',
cushbu_users.last_name
) AS artist_name , count(cushbu_mark_user_favorites.id) as total_fav
FROM
cushbu_mark_user_favorites
LEFT JOIN cushbu_art ON cushbu_art.id=cushbu_mark_user_favorites.art_id
LEFT JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id
WHERE cushbu_mark_user_favorites.user_id=1
GROUP BY cushbu_art.id
But it returns 但它返回
art_id artist_name total_fav
1 sh s 1
2 sb s 1
returns total_fav
only 1
for each row but the excepted output 2
每行仅返回total_fav
1
,但例外输出2
The problem is that you are filtering WHERE cushbu_mark_user_favorites.user_id=1
so it's not possible to get the number of favorites from other users. 问题是您正在过滤WHERE cushbu_mark_user_favorites.user_id=1
因此无法从其他用户那里获得收藏夹数。 The idea is to join the table a second time, but without this contraints. 这个想法是第二次加入该表,但是没有这个矛盾。 Speculating, untested... 推测,未经测试...
SELECT
cushbu_art.id AS art_id,
cushbu_art.title,
cushbu_art.image_name,
CONCAT(
cushbu_users.first_name,
' ',
cushbu_users.last_name
) AS artist_name , b.favorites_count as total_fav
FROM
cushbu_mark_user_favorites
LEFT JOIN cushbu_art ON cushbu_art.id=cushbu_mark_user_favorites.art_id
LEFT JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id
LEFT JOIN (SELECT art_id,count(*) as favorites_count FROM cushbu_mark_user_favorites GROUP BY art_id) as b ON b.art_id=cushbu_art.id
WHERE cushbu_mark_user_favorites.user_id=1
GROUP BY cushbu_art.id
Since you are doing GROUP BY cushbu_art.id, that is why is it returning 1. For your desired output, you can do a subquery , getting the count seperately. 由于您正在执行GROUP BY cushbu_art.id,因此它返回1的原因。对于所需的输出,您可以执行子查询,单独获取计数。
`SELECT
cushbu_art.id AS art_id,
cushbu_art.title,
cushbu_art.image_name,
CONCAT(
cushbu_users.first_name,
' ',
cushbu_users.last_name
) AS artist_name , (select count(*) from cushbu_mark_user_favorites a where a.id=b.id ) as total_fav
FROM
cushbu_mark_user_favorites b
LEFT JOIN cushbu_art ON cushbu_art.id=cushbu_mark_user_favorites.art_id
LEFT JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id
WHERE cushbu_mark_user_favorites.user_id=1
GROUP BY cushbu_art.id
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.