简体   繁体   中英

MYSQL select distinct does not show all records

I have a table containing data like:

customers_id | date_altered
1            | 2012-04-06
1            | 2013-04-09
2            | 2014-02-02
...

and so on. There are around 12k records. I want to get the last date_altered for each customer. I am using the follow query:

SELECT DISTINCT `customers_id`, max(date_altered) FROM bp_booking

which returns only 1 row. Any suggestions?

You want a group by , not distinct :

SELECT customers_id, max(date_altered)
FROM bp_booking
GROUP BY customers_id;
SELECT DISTINCT `customers_id`,
max(date_altered)
FROM bp_booking
group by `customers_id`

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