I am working on a concert promoter's website and I have to retrieve the first, last and nearest future date for shows on each tour. I am attempting to do this with the following query:
SELECT tour.*, min(date) AS `startdate`, max(date) AS finaldate
FROM tour, tour_date
LEFT JOIN (
SELECT tour_id, min(date) AS nextdate
FROM tour_date
WHERE date>=CURDATE()
GROUP BY tour_id
) AS ndtable
ON ndtable.tour_id=tour_date.tour_id
WHERE tour.id=tour_date.tour_id
GROUP BY tour_date.tour_id
ORDER BY nextdate
I am getting the left table results but the "nextdate" column is not showing.
SELECT tour.*, MIN(td.date) AS startdate, MAX(td.date) AS lastdate, MIN(ntd.date) AS nextdate FROM tour JOIN tour_date AS td ON tour.id=td.tour_id JOIN tour_date AS ntd ON tour.id=ntd.tour_id WHERE ntd.date>CURDATE() GROUP BY td.tour_id ORDER BY nextdate
which I think is a lot neater and efficient :)
SELECT tour.*, min(date) AS `startdate`, max(date) AS finaldate, ndtable.nextdate
FROM tour, tour_date
LEFT JOIN (
SELECT tour_id, min(date) AS nextdate
FROM tour_date
WHERE date>=CURDATE()
GROUP BY tour_id
) AS ndtable
ON ndtable.tour_id=tour_date.tour_id
WHERE tour.id=tour_date.tour_id
GROUP BY tour_date.tour_id, ndtable.nextdate
ORDER BY nextdate
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.