mysql - 尝试对另一个 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;
解决方案
你可以使用
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;
足以得到计数
推荐阅读
- xamarin - 在 Xamarin 表单中展开 ListView
- django - 在 views.py 文件中使用 SMTP 时,它显示 CSRF 令牌错误。我已将 EMAIL_USE_TLS 启用为 true
- c++ - 如何在 libexif 中保存 char* 标签值
- android - 尽管对象存在 Kotlin\Room,但 SELECT 查询返回 null
- android - 变量未在侦听器内设置?
- java - Intellij 和堆大小最小的问题
- azure - 使用流分析将数据从 IoT 中心传输到 Cosmos DB 时出现分区错误
- java - 如何使用 IntWritable 的值作为条件对数据进行分区?
- jquery - 在 Laravel 中使用 AJAX 向我的数据库发送数据时出现问题
- wordpress - 如何制作两列垂直的字段?