- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
tb_supporters:
user_id support_date
-----------------------------
1 1301283373
2 1301283743
3 1301799207
1 1403862904
2 1405174895
3 1415266390
作为查询的结果,它应该输出每个唯一 user_id 的最高 support_date。
我的实际查询:
"SELECT DISTINCT(user_id), support_date
FROM tb_supporters WHERE
support_date < " . (time() - 60*60*24*7)
但是,它不会选择 user_id 旁边最高的 support_date。尝试了 MAX(support_date)
、GROUP BY user_id
、ORDER BY support_date DESC
,但没有任何帮助,它仍然只选择最低的 support_date。
能否请您引导我进入正确的方向,如何完成此任务以仅选择每个 user_id 旁边的最高 support_date?
结果应该是:
user_id support_date
-----------------------------
1 1403862904
2 1405174895
3 1415266390
我查询中的 WHERE 子句是为了排除 user_ids,其中 support_date 在过去 7 天的范围内。
重要说明:我根本不想选择这 7 天范围内的现有 user_id,即使他们的支持日期最长也是如此。
最佳答案
The WHERE clause in my query is to keep out user_ids where the support_date is inside a range of the past 7 days.
你可以改写成这样:
The WHERE clause in my query is to select user_ids where highest support_date is outside a range of the past 7 days.
您可以将 GROUP BY
和 MAX
与 HAVING
子句一起使用:
SELECT user_id, MAX(support_date)
FROM tb_supporters
GROUP BY user_id
HAVING MAX(support_date) < UNIX_TIMESTAMP(CURRENT_TIMESTAMP - INTERVAL 7 DAY)
请注意,您不能为此使用 WHERE
子句,因为它会消除用户支持在过去 7 天内的行;不是组。
关于mysql - 在 support_date WHERE 中选择最高值的 DISTINCT user_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007559/
我是一名优秀的程序员,十分优秀!