【问题标题】:#1241 - Operand should contain 1 column(s) how to fix? [duplicate]#1241 - 操作数应包含 1 列如何修复? [复制]
【发布时间】:2013-07-04 09:27:17
【问题描述】:

大家好,我在mysql中遇到了这个错误,查询如下,我不知道哪里出错了,谢谢你的帮助:)

SELECT a.*,b.*,users.*,
    (SELECT p.msg_text,p.occured_at 
    FROM message_private p 
    WHERE p.group_id=a.group_id 
    ORDER BY p.occured_at DESC LIMIT 1) as message,
    f.countf,message.occured_at
FROM message_group a
INNER JOIN message_group b ON a.group_id=b.group_id
INNER JOIN users ON users.profile_id = b.profile_id
LEFT JOIN 
(
    SELECT COUNT(profile_id) countf, id_group
    FROM message_view
    WHERE profile_id = 'sN07X2'
    GROUP BY id_group
) f
  on f.id_group = b.group_id
WHERE a.profile_id = 'sN07X2'
    AND b.profile_id != a.profile_id 
    AND countf > 0 
ORDER BY p.occured_at DESC 
LIMIT 9

【问题讨论】:

    标签: mysql mysql-error-1241


    【解决方案1】:

    查询中的字段列表是

    SELECT a.*,b.*,users.*,
        (SELECT p.msg_text,p.occured_at 
        FROM message_private p 
        WHERE p.group_id=a.group_id 
        ORDER BY p.occured_at DESC LIMIT 1) as message,
        f.countf,message.occured_at
    

    这将子查询的结果别名为message

    但是,子查询选择了两列。

    【讨论】:

      【解决方案2】:

      这个:

       (SELECT p.msg_text,p.occured_at 
          FROM message_private p 
          WHERE p.group_id=a.group_id 
          ORDER BY p.occured_at DESC LIMIT 1) as message
      

      应该返回 1 值,它返回 2 这就是错误发生的原因

      解决方案 1:

      CONCAT(p.msg_text,p.occured_at )
      

      解决方案 2:

      有 2 个子查询,一个用于 p.msg_text,一个用于 p.occured_at

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2021-03-11
        • 2013-10-04
        相关资源
        最近更新 更多