我正在一家音乐会主办方的网站上工作,我必须检索每次巡演演出的第一个、最后一个和最近的 future 日期。我尝试使用以下查询来执行此操作:
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
我得到了左表结果,但“下一个日期”列没有显示。
<小时/> 编辑:我忘了提及此查询仅与将来的演出相关,因此我能够像这样重新排列它: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
我认为这更加整洁和高效:)
最佳答案
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
关于mysql - 左连接与子选择 - 右表列不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007087/