0

下面是我的表格,一个用户可以有多个特定语言的个人资料,非英语个人资料具有更高的优先级。

+---------+--------+----------------+------------ ----+
|ProfileID |用户ID |ProfileLanguage |ProfilePriority |
+---------+--------+----------------+------------ ----+
|1 |1 |zh-CN |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 |zh-CN |2 |
+---------+--------+----------------+------------ ----+


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

+---------+--------+----------------+------------ ----+
|ProfileID |用户ID |ProfileLanguage |ProfilePriority |
+---------+--------+----------------+------------ ----+
|2 |1 |es-MX |1 |
+---------+--------+----------------+------------ ----+
|5 |2 |ja-JP |2 |
+---------+--------+----------------+------------ ----+
|7 |3 |zh-CN |2 |
+---------+--------+----------------+------------ ----+


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

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

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

4

4 回答 4

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
于 2008-12-18T08:08:15.020 回答
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

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

于 2008-12-18T08:07:15.040 回答
0

在我看来,虽然没有您的表格和一些示例数据还不清楚,但您的问题可以通过扩展分组来解决,如下所示:

SELECT UserID, ProfileID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'en-US' OR ProfilePriority = 2
GROUP BY UserID, ProfileID
于 2008-12-18T08:08:21.870 回答
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
于 2008-12-18T09:49:25.067 回答