SELECT DISTINCT user_id support_date WHERE 中的最高值
SELECT DISTINCT user_id of highest value in support_date WHERE
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)
但是,它并不是 select除 user_id 之外最高的 support_date。
尝试了 MAX(support_date)
、GROUP BY user_id
、ORDER BY support_date DESC
,但没有任何帮助,它仍然是 select 最低的 support_date。
请你引导我进入正确的方向如何完成这个任务 select 只有每个 user_id 旁边最高的 support_date?
结果应该是:
user_id support_date
-----------------------------
1 1403862904
2 1405174895
3 1415266390
我查询中的 WHERE 子句是为了排除 user_ids,其中 support_date 在过去 7 天的范围内。
重要说明: 在这 7 天的范围内存在 user_id 我根本不想被 select 编辑,即使是他们最高 support_date.
样本Table
CREATE Table table_sample(id INT, date INT);
一些 INSERT 的
INSERT INTO table_sample VALUES(1,1301283373);
INSERT INTO table_sample VALUES(2,1301283743);
INSERT INTO table_sample VALUES(3,1301799207);
INSERT INTO table_sample VALUES(1,1403862904);
INSERT INTO table_sample VALUES(2,1405174895);
INSERT INTO table_sample VALUES(3,1415266390);
SELECT * FROM table_sample;
+------+------------+
| id | date |
+------+------------+
| 1 | 1301283373 |
| 2 | 1301283743 |
| 3 | 1301799207 |
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------+------------+
6 rows in set (0.00 sec)
Your query
SELECT id,MAX(date) FROM table_sample GROUP BY id;
+------------+------+
| id |MAX(date) |
+------------+------+
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------------+------+
3 rows in set (0.00 sec)
要查找日期在过去 7 天的范围内,您可以
SELECT id,MAX(date) FROM table_sample WHERE date <= (UNIX_TIMESTAMP() - (60 * 60 * 24 * 7)) GROUP BY id;
+------+------------+
| id | MAX(date) |
+------+------------+
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------+------------+
3 rows in set (0.00 sec)
试试这样的东西:
SELECT user_id,max(support_date) as support_date
FROM mytable
GROUP BY user_id
ORDER BY support_date DESC
SELECT id, max(date) FROM tb_supporters group by id;
您也可以使用默认的 MySQL unix 时间戳函数
SELECT
user_id, MAX(support_date)
FROM
tb_supporters
WHERE
support_date < (UNIX_TIMESTAMP() - (60 * 60 * 24 * 7))
GROUP BY 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 天内;不是 组.
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)
但是,它并不是 select除 user_id 之外最高的 support_date。
尝试了 MAX(support_date)
、GROUP BY user_id
、ORDER BY support_date DESC
,但没有任何帮助,它仍然是 select 最低的 support_date。
请你引导我进入正确的方向如何完成这个任务 select 只有每个 user_id 旁边最高的 support_date?
结果应该是:
user_id support_date
-----------------------------
1 1403862904
2 1405174895
3 1415266390
我查询中的 WHERE 子句是为了排除 user_ids,其中 support_date 在过去 7 天的范围内。
重要说明: 在这 7 天的范围内存在 user_id 我根本不想被 select 编辑,即使是他们最高 support_date.
样本Table
CREATE Table table_sample(id INT, date INT);
一些 INSERT 的
INSERT INTO table_sample VALUES(1,1301283373);
INSERT INTO table_sample VALUES(2,1301283743);
INSERT INTO table_sample VALUES(3,1301799207);
INSERT INTO table_sample VALUES(1,1403862904);
INSERT INTO table_sample VALUES(2,1405174895);
INSERT INTO table_sample VALUES(3,1415266390);
SELECT * FROM table_sample;
+------+------------+
| id | date |
+------+------------+
| 1 | 1301283373 |
| 2 | 1301283743 |
| 3 | 1301799207 |
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------+------------+
6 rows in set (0.00 sec)
Your query
SELECT id,MAX(date) FROM table_sample GROUP BY id;
+------------+------+
| id |MAX(date) |
+------------+------+
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------------+------+
3 rows in set (0.00 sec)
要查找日期在过去 7 天的范围内,您可以
SELECT id,MAX(date) FROM table_sample WHERE date <= (UNIX_TIMESTAMP() - (60 * 60 * 24 * 7)) GROUP BY id;
+------+------------+
| id | MAX(date) |
+------+------------+
| 1 | 1403862904 |
| 2 | 1405174895 |
| 3 | 1415266390 |
+------+------------+
3 rows in set (0.00 sec)
试试这样的东西:
SELECT user_id,max(support_date) as support_date
FROM mytable
GROUP BY user_id
ORDER BY support_date DESC
SELECT id, max(date) FROM tb_supporters group by id;
您也可以使用默认的 MySQL unix 时间戳函数
SELECT
user_id, MAX(support_date)
FROM
tb_supporters
WHERE
support_date < (UNIX_TIMESTAMP() - (60 * 60 * 24 * 7))
GROUP BY 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 天内;不是 组.