mysql - 错误 1241 操作数应包含 1 列子查询工作日

标签 mysql subquery

我正在尝试执行一个 mysql 查询,该查询显示本周(从周一到周日)生产的产品数量。(本例中仅包含 Lunes 和 Martes)

我已经成功地使用单个 id 执行查询,但是当我想向所有人展示时,它给了我错误。

select formatopeso.tipo_formato, (select sum(cantidad) 
from previsionpedidos 
where id_formatopeso = 1 and  WEEKDAY(fecha) = 0) as Lunes, 
(select sum(cantidad) fromprevisionpedidos where id_formatopeso = 1 and  WEEKDAY(fecha) = 1) 
as Martes, 
from previsionpedidos inner join formatopeso on 
previsionpedidos.id_formatopeso = formatopeso.id_formatopeso 
where formatopeso.id_formatopeso= 1 and yearweek(fecha,1) = yearweek(now()) 
group by formatopeso.tipo_formato;

我尝试了这个,但出现错误 ERROR 1241 (21000): Operand should contains 1 columns

select formatopeso.tipo_formato,
(select sum(cantidad) from previsionpedidos inner join 
formatopeso on previsionpedidos.id_formatopeso = formatopeso.id_formatopeso 
where WEEKDAY(fecha) = 0) as lunes,
(select sum(cantidad),fecha from previsionpedidos 
inner join formatopeso on previsionpedidos.id_formatopeso = 
formatopeso.id_formatopeso
where WEEKDAY(fecha) = 1) as Martes from previsionpedidos 
inner join formatopeso on previsionpedidos.id_formatopeso = formatopeso.id_formatopeso 
where yearweek(fecha,1) = yearweek(now()) 
group by formatopeso.tipo_formato;

谢谢

我需要显示类似于以下内容的结果:

+--------------+-------+--------+
| tipo_formato | Lunes | Martes |
+--------------+-------+--------+
| 12Ø 70gr     |   175 |   250  |
| 20Ø 150gr    |   NULL|   NULL |
| 22Ø 180gr    |   NULL|   125  |
| 25Ø 220gr    |   200 |   NULL |
| 28Ø 220gr    |   175 |   250  |
+--------------+-------+--------+

最佳答案

FROM 之前有一个多余的逗号。

SELECT formatopeso.tipo_formato, 
    (SELECT SUM(cantidad) 
     FROM previsionpedidos 
     WHERE id_formatopeso = 1 
        AND  WEEKDAY(fecha) = 0
    ) as Lunes, 
    (SELECT SUM(cantidad) 
    FROM previsionpedidos 
    WHERE id_formatopeso = 1 
        AND WEEKDAY(fecha) = 1
    ) as Martes 
FROM previsionpedidos 
INNER JOIN formatopeso on previsionpedidos.id_formatopeso = formatopeso.id_formatopeso 
WHERE formatopeso.id_formatopeso=1 
    AND yearweek(fecha,1) = yearweek(now()) 
GROUP BY formatopeso.tipo_formato;

关于mysql - 错误 1241 操作数应包含 1 列子查询工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46410736/

相关文章:

sql-server - 从 SQL 中的子查询设置别名

MySQL同时选择多个表的几列而不使用JOIN

php - 如何在单个查询中使用 While 循环编写多个查询?

mysql - 在 Laravel 子查询中使用 count 编写 where 子句

mysql - 在一个 SQL 查询中选择 COUNT(id) 和总计

php - 在查询中加入第二个表不会在 WordPress 中返回任何内容

mysql - 为什么 MySQL union all 对 "practically"相同的查询显示不同的结果?

c++ - MySQL插入序​​列的优化

mysql - 快速搜索庞大的mysql数据库

mysql - 使用一对多关系加入表会产生奇怪的结果