SQL表达式问题

时间:2008-12-18 08:02:34

标签: sql group-by expression

以下是我的表格,用户可以使用某些语言的多个个人资料,非英语个人资料的优先级更高。

+----------+--------+----------------+----------------+
|ProfileID |UserID  |ProfileLanguage |ProfilePriority |
+----------+--------+----------------+----------------+
|1         |1       |en-US           |2               |
+----------+--------+----------------+----------------+
|2         |1       |es-MX           |1               |
+----------+--------+----------------+----------------+
|3         |1       |ja-JP           |1               |
+----------+--------+----------------+----------------+
|4         |2       |es-MX           |1               |
+----------+--------+----------------+----------------+
|5         |2       |ja-JP           |2               |
+----------+--------+----------------+----------------+
|6         |2       |de-DE           |1               |
+----------+--------+----------------+----------------+
|7         |3       |en-US           |2               |
+----------+--------+----------------+----------------+


例如:当讲西班牙语的访问者请求我的网站(其中ProfileLanguage ='es-MX'或ProfilePriority = 2)时,我想要如下记录:

+----------+--------+----------------+----------------+
|ProfileID |UserID  |ProfileLanguage |ProfilePriority |
+----------+--------+----------------+----------------+
|2         |1       |es-MX           |1               |
+----------+--------+----------------+----------------+
|5         |2       |ja-JP           |2               |
+----------+--------+----------------+----------------+
|7         |3       |en-US           |2               |
+----------+--------+----------------+----------------+


下面是获取用户的基本SQL:

SELECT UserID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'es-MX' OR ProfilePriority = 2
GROUP BY UserID

但是如你所知,我只能获得UserID,但我还需要其他列信息,比如ProfileID等。所以我希望这里的专家可以告诉我正确的SQL表达式来获得正确的记录。

4 个答案:

答案 0 :(得分:3)

如果profilepriority和userid可以是复合唯一键,这可能有效;

select p.* from Profile p join 
(SELECT UserID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'en-US' OR ProfilePriority = 2
GROUP BY UserID) tt
on p.userID = tt.UserID and p.ProfilePriority = tt.ProfilePriority

答案 1 :(得分:1)

SELECT *
FROM Profile as tb1 inner join
(SELECT UserID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'es-MX' OR ProfilePriority = 2
GROUP BY UserID) as tb2 on 
tb1.userid = tb2.userid and tb1.ProfilePriority = tb2.ProfilePriority

在上述查询中输入您用逗号分隔的所有列,而不是*。

答案 2 :(得分:0)

在我看来,虽然没有你的表格和一些样本数据就不清楚,只需按照以下方式扩展你的分组就可以解决你的问题:

SELECT UserID, ProfileID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'en-US' OR ProfilePriority = 2
GROUP BY UserID, ProfileID

答案 3 :(得分:0)

如果profilepriority和profilelanguage都正确,我猜你有一个优先级。所以这可能有助于那种情况;

select p.* from
(select tt1.userid, min(tt.pr) pr from 
(
SELECT profileid, UserID, MIN(ProfilePriority) AS ProfilePriority, 1 as pr
FROM Profile
WHERE ProfileLanguage = 'en-US' and ProfilePriority = 2
GROUP BY profileid, UserID

union 

SELECT profileid, UserID, MIN(ProfilePriority) AS ProfilePriority, 2 as pr
FROM Profile
WHERE ProfileLanguage = 'en-US' or ProfilePriority = 2
GROUP BY profileid, UserID

) tt1
group by userid) tt2    
join
(
SELECT profileid, UserID, MIN(ProfilePriority) AS ProfilePriority, 1 as pr
FROM Profile
WHERE ProfileLanguage = 'en-US' and ProfilePriority = 2
GROUP BY profileid, UserID

union 

SELECT profileid, UserID, MIN(ProfilePriority) AS ProfilePriority, 2 as pr
FROM Profile
WHERE ProfileLanguage = 'en-US' or ProfilePriority = 2
GROUP BY profileid, UserID
) tt3
on tt2.userId = tt3.userId and tt2.pr = tt3.pr
join profile p on p.profileid = tt3.profileid