MySQL 子查询返回多行

标签 mysql subquery

SELECT CONCAT(MONTH(revenue_date),"/ ", YEAR(revenue_date)) as month_year, 

    (SELECT COUNT(DISTINCT DATE(delivery_date)) as operating_days
    FROM table2 where weekday(delivery_date) <> 5 and     
    weekday(delivery_date) <> 6 and delivery_date < current_time and     
    year(delivery_date) > 2015 GROUP BY YEAR(delivery_date), MONTH(delivery_date)), 
sum(revenue) as total_revenue,
from table1 
where revenue_date < current_time and year(revenue_date) > 2015
group by year(revenue_date), month(revenue_date);

这显示错误“子查询返回超过 1 个值”。如果我不在子查询中包含 group by ,它只会返回所有月份的总计,但我需要它单独返回所有月份。

最佳答案

试试这个:

SELECT CONCAT(MONTH(revenue_date),"/ ", YEAR(revenue_date)) as month_year, a.operating_days
sum(revenue) as total_revenue,
from table2 JOIN
(SELECT YEAR(delivery_date) as `year`, MONTH(delivery_date) as `month`, COUNT(DISTINCT DATE(delivery_date)) as operating_days
    FROM table2 where weekday(delivery_date) <> 5 and     
    weekday(delivery_date) <> 6 and delivery_date < current_time and     
    year(delivery_date) > 2015 GROUP BY YEAR(delivery_date), MONTH(delivery_date)) a
ON year(revenue_date) = a.year AND month(revenue_date) = a.month
where revenue_date < current_time and year(revenue_date) > 2015
group by year(revenue_date), month(revenue_date);

关于MySQL 子查询返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707918/

相关文章:

mysql - 同时查询多个表的特定日期范围

PHP MySQL 选择查询

php - PDO 库是否比原生 MySQL 函数更快?

mysql - Laravel 具有慢速 subSelect 查询(MySQL)

sql - 新手问题 : Problem with results, sql, join, where, "<"操作符

mysql - 使用子查询删除 MySQL

php - cakephp3根据类型将一个表链接到多个表

php - mysql 自动增量 id 与 php 生成 id

sql - 内连接中的子查询

mysql - 计算mysql表的有限行数之和?