我已製備的樣品。我幾乎可以肯定,這是你所搜索的。 我將描述它的一些碎片
完整查詢
SELECT count(`art_id`) as cnt, `art_id`
FROM user_art
WHERE `user_id` in (
SELECT DISTINCT `user_id`
FROM user_art
WHERE `art_id` = 10 -- Current Article ID to find other user
AND user_id <> 20 -- Current User Id to exclude from list
)
GROUP by `art_id`
ORDER BY cnt DESC , `art_id` DESC
LIMIT 1;
分步
輸入數據
mysql> SELECT * FROM user_art;
+----+---------+--------+
| id | user_id | art_id |
+----+---------+--------+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 20 | 80 |
| 4 | 10 | 90 |
| 5 | 20 | 10 |
| 6 | 23 | 10 |
| 7 | 23 | 11 |
| 8 | 23 | 12 |
| 9 | 10 | 11 |
+----+---------+--------+
9 rows in set (0,00 sec)
mysql>
找到所有用戶ID誰也讀SAM文章
mysql> select DISTINCT `user_id`
-> FROM user_art
-> WHERE `art_id` = 10 -- Current Article ID to find other user
-> AND user_id <> 20; -- Current User Id to exclude from list
+---------+
| user_id |
+---------+
| 10 |
| 23 |
+---------+
2 rows in set (0,00 sec)
mysql>
發現,通過用戶列表
閱讀所有文章發現,由用戶說明和文章ID DESC =>較高article_no的數量由用戶名單和順序閱讀所有文章=>新的文章
mysql> SELECT count(`art_id`) as cnt, `art_id`
-> FROM user_art
-> WHERE `user_id` in (10,23)
-> GROUP by `art_id`
-> ORDER BY cnt DESC , `art_id` DESC
-> ;
+-----+--------+
| cnt | art_id |
+-----+--------+
| 2 | 11 |
| 2 | 10 |
| 1 | 90 |
| 1 | 12 |
+-----+--------+
4 rows in set (0,00 sec)
mysql>
兩者comination查詢
comination與同時限制查詢只得到在下一篇文章
mysql> SELECT count(`art_id`) as cnt, `art_id`
-> FROM user_art
-> WHERE `user_id` in (
-> SELECT DISTINCT `user_id`
-> FROM user_art
-> WHERE `art_id` = 10 -- Current Article ID to find other user
-> AND user_id <> 20 -- Current User Id to exclude from list
-> )
-> GROUP by `art_id`
-> ORDER BY cnt DESC , `art_id` DESC
-> LIMIT 1;
+-----+--------+
| cnt | art_id |
+-----+--------+
| 2 | 11 |
+-----+--------+
1 row in set (0,00 sec)
mysql>
後所需的輸出,以及看看有什麼預期爲好。 – Rahul
@Rahul檢查更新並請讓我知道是否清楚 – Yassine
看來你需要工作一些mysql教程網站。 – Drew