1
所以我有一個場景,其中有1-8個人,我需要查詢他們「喜歡」每人3件事。我有查詢設置爲如何使用每個人的MYSQL LIMIT
SELECT liked FROM likeTable WHERE uid IN (uid1,uid2,uid3,uid4) LIMIT 12
但很明顯,當我有12本有可能阻止「喜歡」爲UID1,留下,其餘的爲0。我讀了可能的解決方案是使用UNION ALL例如...
(SELECT liked FROM likeTable WHERE uid = uid1 LIMIT 3)
UNION ALL
(SELECT liked FROM likeTable WHERE uid = uid2 LIMIT 3)
UNION ALL
(SELECT liked FROM likeTable WHERE uid = uid3 LIMIT 3)
UNION ALL
(SELECT liked FROM likeTable WHERE uid = uid4 LIMIT 3)
,我會能夠通過一個for循環使得SQL查詢字符串在PHP來實現這一點,但是這是我的查詢數據的有效方法是什麼?
注:我真的不關心其中的「喜歡」被檢索的順序,雖然這將是很好,如果我可以添加一個ORDER BY likeID DESC,這是我的自動增量列
謝謝!
可能的重複http://stackoverflow.com/questions/2000744/mysql-limit-results-per-category和http://stackoverflow.com/questions/2129693/mysql-using-limit-within-group-按組獲得n個結果 –
Try:'SET @cnt:= 0; SELECT b.liked FROM likeTable a \t INNER JOIN like表b ON a.uid = b.uid AND(@cnt:= @cnt - 1)> 0 WHERE uid IN(uid1,uid2,uid3,uid4)AND @ cnt:= 3 LIMIT 12' – Dor