1
我有以下MySQL查詢SUM與CASE語句在MySQL
SELECT c.`id`
,c.`category_name`
,c.`category_type`
,c.bookmark_count
,f.category_id cat_id
,f.unfollow_at
,(
CASE WHEN c.id = f.follower_category_id
THEN (
SELECT count(`user_bookmarks`.`id`)
FROM `user_bookmarks`
WHERE (`user_bookmarks`.`category_id` = cat_id)
AND ((`f`.`unfollow_at` > `user_bookmarks`.`created_at`) || (`f`.`unfollow_at` = '0000-00-00 00:00:00'))
)
ELSE 0 END
) counter
,c.id
,f.follower_category_id follow_id
,c.user_id
FROM categories c
LEFT JOIN following_follower_categories f ON f.follower_category_id = c.id
WHERE c.user_id = 26
ORDER BY `category_name` ASC
,這裏是什麼輸出我得到後execuation
現在我只想計數。這裏我有具有值字段id反對我有計數器30,3,2和BOOKMARK_COUNT是(我需要包括僅一次)
,我接受輸出ID 是30 + 3 + 2 + 4(bookmark_count只有一次)。
我不知道如何做到這一點。
任何人都可以幫我
非常感謝
你想返回3行中的id = 172(全部具有BOOKMARK_COUNT = 4和反= 39),還是隻有一行? – yalpertem 2014-09-03 12:57:32