我正在制作音乐会发起人的网站,我必须在每次巡演中检索演出的第一个,最后一个和最近的未来日期。我试图使用以下查询执行此操作:
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
我正在获得左表结果,但是" nextdate"列未显示。
<小时/> 编辑:我忘了提到这个查询仅适用于将来有节目的游览,所以我能够像这样重新排列:
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
我觉得它更整洁有效:)
答案 0 :(得分:0)
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