简体   繁体   English

左联接与子选择-右表列未显示

[英]Left Joining with a subselect - right table columns not showing

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. 我正在获取左侧表格的结果,但未显示“ nextdate”列。


EDIT: I forgot to mention that this query is only relevant for tours with shows in the future, so I was able to rearrange it like this: 编辑:我忘了提及此查询仅与将来的演出巡回演出相关,因此我能够像这样重新排列它:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM