简体   繁体   中英

Order and group by date in Mysql

Hi all and thanks in advance. I have a small problem which can not resolve, I have this table, and I want to sort by date and group it (but I only show 1 row per idCAT)

| id | idcat | name |     date   |

| 1  | 3     | xx   | 2011-01-02 |

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

| 5  | 4     | ce   | 2011-01-06 |

would like to stay that way, try in a way but I can not

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

Order by ID


Thank's a one friend the work.

SELECT id, idcat, name, date FROM (SELECT * FROM data ORDER BY idcat, date ) m GROUP BY idcat

I can't test conveniently atm, but try this:

SELECT FIRST(id), idcat, FIRST(name), FIRST(date) AS d FROM myTable GROUP BY idcat ORDER BY d;

Note the use of the FIRST calls to pick the first row in the table with any particular idcat.

If you are trying to get groupings by the idcat, then sorted by date:

select id, idcat, name, date from myTable group by idcat order by date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM