要使用什么查询在下面给出的表格中给出了输出?

时间:2016-07-07 07:37:11

标签: mysql

数据库是

+---+-----+------+------------+
| x | y   | z    | d          |
+---+-----+------+------------+
| 1 | abc | you  | 2016-07-11 |
| 1 | ac  | you  | 2016-07-12 |
| 2 | boo | game | 2016-07-13 |
| 2 | boo | hoho | 2016-07-14 |
| 2 | foo | game | 2016-07-21 |
| 2 | goo | gzz  | 2016-07-31 |
| 3 | gaa | bha  | 2016-07-05 |
+---+-----+------+------------+

我希望输出有

------------------------------
|  x  |  y  |  z  |      d   |
------------------------------
|  1  | ac  | you |2016-07-12|
|  2  | foo | game|2016-07-21|
|  3  | gaa | bha |2016-07-05|
------------------------------

这是什么查询我是mysql的新手。请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

您似乎希望为x中具有最新日期d的每个值选择记录。如果是这样,那么以下查询应该有效:

SELECT t1.x, t1.y, t1.z, t1.d
FROM yourTable t1
INNER JOIN
(
    SELECT x, MAX(d) AS d
    FROM yourTable
    GROUP BY x
) t2
    ON t1.x = t2.x AND t1.d = t2.d