【问题标题】:SQL expression problemSQL表达式问题
【发布时间】:2010-09-27 11:45:03
【问题描述】:

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

+---------+--------+----------------+------------ ----+ |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表达式以获取正确的记录。

【问题讨论】:

  • 我相信,在您的结果表中,PROFILE_ID = 2 的第一条记录不符合给定条件。
  • 致 Adiel A.:对不起,这是一个写作错误,我刚刚更正了,条件应该是“其中 ProfileLanguage = 'es-MX' 或 ProfilePriority = 2”
  • 一个更正的地方...... .猜猜是哪儿。 . .. 在您的查询中 ;)
  • 再次抱歉,已更正。谢谢你提醒我;)

标签: sql group-by expression


【解决方案1】:

如果 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

【讨论】:

  • @todownvoteman:你能解释一下为什么投了反对票,让我学习正确答案吗?
  • 感谢您的回答,但它并没有像我预期的那样工作,它仍然返回多个 UserID。
【解决方案2】:
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

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

【讨论】:

  • 谁删除了我的评论而不是给出答案?
  • 给 Smiksha,我试过了,但它不起作用,它仍然返回具有相同 UserID 的多个项目的结果。
【解决方案3】:

在我看来,虽然没有您的表格和一些示例数据并不清楚,但您的问题只需按如下方式扩展分组即可解决:

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

【讨论】:

  • 可能会得到两条 USER_ID 相同但 PROFILE_ID 不同的记录。我希望你明白了。
  • 是的,我有多个 UserID,我希望结果只返回唯一的 UserID。
【解决方案4】:

如果 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

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2023-03-09
    • 2020-02-21
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多