[英]Get values of first record and last record by date in mysql subquery
I know that for some MySQL pro, this is reasonably straightforward. 我知道对于某些MySQL专业人士来说,这相当简单。 I further realize that the answer could likely be figured out from other answers , however I've spent some real time trying to build this query, and I can't seem to figure out how to apply those solutions to my situation. 我进一步意识到,答案很可能可以从其他答案中找出来 ,但是我花了一些时间尝试构建此查询,而且我似乎无法弄清楚如何将这些解决方案应用于我的情况。
Mine seems different than others who want the "min and max" of a field - but I need the value from another field based on the "min and max" of the date field. 我的似乎与其他人想要一个字段的“最小和最大”不同-但我需要基于日期字段的“最小和最大” 另一个字段的值。
Given the following structure - a "user" table, and an "entries" table: 给定以下结构-一个“用户”表和一个“条目”表:
Data Sample (for "entries" table): 数据样本(用于“条目”表):
id | user_id | date | value
---+---------+--------------+-------
1 1 2018-02-01 125
2 5 2018-01-15 220
3 1 2017-12-31 131
4 4 2018-01-01 77
3 1 2017-12-15 133
I'd like to know value of the first entry (by date) , the value of the last entry (by date), and the user_id. 我想知道第一个条目的值(按日期), 最后一个条目的值(按日期)和user_id。
The results should be: 结果应为:
user_id | first_date | first_value | last_date | last_value
--------+------------+-------------+------------+-----------
1 2017-12-15 133 2018-02-01 125
4 2018-01-01 77 2018-01-01 133
5 2018-01-15 220 2018-01-15 220
While I want the best solution, what I've been working on revolves around combining some queries like so: 虽然我想要最好的解决方案,但我一直在努力围绕合并一些查询,如下所示:
SELECT user_id, l.date AS last_date, l.value AS last_value, f.date AS first_date, f.value AS first_value
FROM user AS u
LEFT JOIN (SELECT user_id, date, value FROM entries ORDER BY date ASC LIMIT 1) AS f ON f.user_id = u.user_id
LEFT JOIN (SELECT user_id, date, value FROM entries ORDER BY date DESC LIMIT 1) AS l ON l.user_id = u.user_id
NOTE: This doesn't work. 注意:这不起作用。 If I wanted the "first entry" for someone, I would write a query that was SELECT user_id, date, value FROM entries ORDER BY date ASC LIMIT 1
- however, using it in the subqueries doesn't have the desired effect. 如果我想要某人的“第一个条目”,我将编写一个查询,该查询为SELECT user_id, date, value FROM entries ORDER BY date ASC LIMIT 1
但是,在子查询中使用它并没有取得预期的效果。
I've also tried some GROUP BY
queries, with no success as well. 我也尝试了一些GROUP BY
查询,但也没有成功。
SQL Fiddle: http://sqlfiddle.com/#!9/71599 SQL小提琴: http ://sqlfiddle.com/#! 9/71599
The following query gives you the expected result, but it's done without using a LEFT JOIN
. 以下查询为您提供了预期的结果,但无需使用LEFT JOIN
。 So the NULL
values are excluded. 因此,将排除NULL
值。
SELECT
u.id AS user_id,
e1.date AS first_date,
e1.value AS first_value,
e2.date AS last_date,
e2.value AS last_value
FROM
users u,
(SELECT * FROM entries e ORDER BY date ASC) e1,
(SELECT * FROM entries e ORDER BY date DESC) e2
WHERE
e1.user_id = u.id
AND
e2.user_id = u.id
GROUP BY
u.id
And here's a working fiddle - http://sqlfiddle.com/#!9/71599/8 这是一个有效的小提琴-http://sqlfiddle.com/#! 9/71599/8
Also, it's worth noting that the LIMIT
in your attempt would limit the results to 1 for all joined results, not each joined result. 另外,值得注意的是,您尝试执行LIMIT
会将所有合并结果(而不是每个合并结果)的结果限制为1。 Either way, the LEFT JOIN
didn't work. 无论哪种方式, LEFT JOIN
都不起作用。 If anyone knows why, I'd be interested to understand. 如果有人知道为什么,我很想了解。
Edit: Here's another attempt, this time utilising MIN()
and MAX()
, rather than ORDER BY
. 编辑:这是另一种尝试,这次使用MIN()
和MAX()
,而不是ORDER BY
。 Unfortunately, you need to join the entries
table multiple times for this to work though. 不幸的是,您需要多次连接entries
表才能使它起作用。
SELECT
u.id AS user_id,
e1.date AS first_date,
e1.value AS first_value,
e2.date AS last_date,
e2.value AS last_value
FROM users u
INNER JOIN entries e1 ON (u.id = e1.user_id)
INNER JOIN entries e2 ON (u.id = e2.user_id)
INNER JOIN (
SELECT user_id, MIN(date) AS date
FROM entries
GROUP BY user_id
) e3 ON (e1.user_id = e3.user_id AND e1.date = e3.date)
INNER JOIN (
SELECT user_id, MAX(date) AS date
FROM entries
GROUP BY user_id
) e4 ON (e2.user_id = e4.user_id AND e2.date = e4.date)
GROUP BY u.id
Another working fiddle: http://sqlfiddle.com/#!9/71599/18 另一个有效的小提琴: http ://sqlfiddle.com/#! 9/71599/18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.