Titan97 Titan97 - 4 months ago 7
SQL Question

What Query To be Used For getting the output has given below on the table given?

The database is

enter image description here

I want the output has

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


What is the query for this I am newbie in mysql. Please help me out with it. Thanks in advance

Answer

It looks like you want to select the record for each value of x which has the latest date in column d. If so, then the following query should work:

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
Comments