ITEM CURRENT TOKENS STATUS DATE/TIME
mypro 0.10/0.10 ongoing 19/05/2015 17:42:03
mypro 0.20/0.10 ongoing 19/05/2015 17:40:57
myproduct3 0.10/0.10 ongoing 19/05/2015 15:53:1
Here I want the first row complete values which is stored recently of same product rather than second product, when I was using group by
cluase I was getting second row values in mysql database.
SELECT * FROM TBL_NAME ORDER BY DATE_TIME DESC LIMIT 1
try this
MySQL knows nothing of first/second row unless you specify it and I'm assuming you want the one the latest DATE/TIME
.
For simple queries this is easily achieved using an ORDER BY
and a LIMIT
:
SELECT *
FROM table
ORDER BY date_time_col DESC /* Latest rows first */
LIMIT 1; /* Fetch only the first row */
For grouped queries this is more difficult, I like the LEFT JOIN
approach:
SELECT *
FROM table t
LEFT JOIN table t1 /* Join to rows of same item & later times */
ON t1.item_col = t.item_col
AND t1.date_time_col > t.date_time_col
WHERE t1.item_col IS NULL; /* Rows that have no later time */
This does assume that item_col
and date_time_col
have unique combinations however.