首页 > 解决方案 > 尝试对另一个 SELECT 命令的结果使用 SELECT 命令

问题描述

我使用这种语法根据日期将数据从一个表连接到另一个表:

SELECT DISTINCT(Incident_DateCopy), wt.dt_iso, weather_main
  FROM `incidentdatatwo` 
  LEFT 
  JOIN `weather` AS wt 
    ON DATE(dt_iso) = Incident_DateCopy

现在,我需要按weather_main列对结果进行计数和分组。尝试了以下方法,但它不起作用:

SELECT `weather_main`
     , COUNT(*) 
  FROM `incidentdatatwo` 
   (SELECT DISTINCT(Incident_DateCopy)
             , wt.dt_iso
             , weather_main 
        FROM `incidentdatatwo` 
        LEFT 
        JOIN `weather` AS wt 
           ON DATE(dt_iso) = Incident_DateCopy) 
     GROUP 
         BY weather_main;

标签: mysql

解决方案


你可以使用

SELECT 
    weather_main,COUNT(*) 
FROM  
(SELECT DISTINCT(Incident_DateCopy), wt.dt_iso, weather_main 
FROM incidentdatatwo 
LEFT JOIN weather AS wt ON DATE(dt_iso) = Incident_DateCopy) a1
GROUP BY weather_main;

SELECT  weather_main,COUNT(*)
FROM incidentdatatwo 
LEFT JOIN weather AS wt ON DATE(dt_iso) = Incident_DateCopy
GROUP BY weather_main;

足以得到计数


推荐阅读