I have a table (a list of cars, years and models) and I am trying to show a list of just the years, but the list of years that manufacture has made a car could be huge. Meaning when I query "Volvo" Im returning a list of all years Volvo has made a car, Using:
"SELECT id, year FROM `models_auto` WHERE `make`=? ORDER BY `year` DESC;"
However, Volvo may have made 50 cars that year and the return is all 50 duplicate years. I want to return just unique list of years, not duplicates. I looked at DISTINCT but using:
"SELECT DISTINCT id, year FROM `models_auto` WHERE `make`=? ORDER BY `year` DESC;"
Still returns everything because ID is unique. How can I only DISTINCT year?
Thank you.
If you want only the years then you can use use GROUP BY
.
SELECT `year`
FROM `models_auto`
WHERE `make`=?
GROUP BY `year`
ORDER BY `year` DESC;"
Or using DISTINCT
:
SELECT DISTINCT `year`
FROM `models_auto`
WHERE `make`=?
ORDER BY `year` DESC;"
One option is to return the years as a comma delimited list, using group_concat()
:
select make, group_concat(distinct year order by year)
from models_auto
group by make;
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.