下面是我的表格,一个用户可以有多个特定语言的个人资料,非英语个人资料具有更高的优先级。
+---------+--------+----------------+------------ ----+ |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表达式以获得正确的记录。