簡體   English   中英

DQL MySQL左聯接查詢排序

[英]DQL MySQL left join query sorting

我有兩個表,一個叫做Ratings the other Users ,等級或多或少像:

userid | depth | rating
1        0      5
1        3      8
1        4      9

如何讓所有用戶都加入他們的評級,為每個用戶選擇最小的深度?

您必須先將第一個表與其他表的主ID聯接在一起,然后才能為此使用帶有元素group by的mysql聚合函數。

SELECT u.*, i.minDepth FROM
Users AS u LEFT JOIN 
(SELECT r.userId, min(r.depth) AS minDepth from Ratings AS r GROUP BY r.userId) AS i 
ON u.userId=i.userId;

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM