【问题标题】:ERROR 1241 Operand should contain 1 column Subquery weekday错误 1241 操作数应包含 1 列子查询工作日
【发布时间】:2017-09-25 17:17:42
【问题描述】:

我正在尝试执行一个 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 contain 1 column(s)

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  |
+--------------+-------+--------+

【问题讨论】:

    标签: mysql subquery


    【解决方案1】:

    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;
    

    【讨论】:

    • 193/5000 感谢朋友的帮助,解决了代码更干净整洁的问题,但是我的请求将能够显示所有带有他的 id 的产品,我只能显示一个
    猜你喜欢
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2021-09-16
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多