select a.*,(select count(id) as d from comment2 where a.id=articleId),(select max(id) as b,max(name) as e from comment2 where a.id=articleId) from article as a where a.classId=1 这是我的SQL语句。。运行时报Operand should contain 1 column(s)但是如果独立运行select max(id) as b,max(name) as e from comment2 where 1=articleId 就不会报错。。请高手帮忙解决一下。。

解决方案 »

  1.   

    select a.*,
    (select count(id) as d from comment2 where a.id=articleId),
    (select max(id) as b from comment2 where a.id=articleId) as b ,
    (select max(name) as e from comment2 where a.id=articleId) as e 
    from article as a where a.classId=1 
      

  2.   

    MySQL 中不允许在这种列上的子查询返回多于1列的数据
      

  3.   

    (select max(id) as b,max(name) as e from comment2 where a.id=articleId)
    子查询只能返回一列
      

  4.   

    分开:
    select a.*,
        (select count(id) as d from comment2 where a.id=articleId),
        (select max(id) as b from comment2 where a.id=articleId)  ,
        (select max(name) as e from comment2 where a.id=articleId) 
    from article as a where a.classId=1 可以优化一下SQL语句
      

  5.   

    select a.*,(select count(id) as d from comment2 where a.id=articleId),(select max(id) as b,max(name) as e from comment2 where a.id=articleId) from article as a where a.classId=1 修改一下就可以了
      

  6.   

    mysql> select max(userid),max(username) from user;
    +-------------+---------------+
    | max(userid) | max(username) |
    +-------------+---------------+
    |           9 | 譏ッ蠕キ蝗ス        |
    +-------------+---------------+
    1 row in set (0.02 sec)
    mysql> select * ,(select max(userid),max(username) from user) from user;
    ERROR 1241 (21000): Operand should contain 1 column(s)
      

  7.   

    mysql> select * ,t.a,t.b from user,(select max(userid) a,max(username) b from
    er) t;
    +--------+--------------------+------+-----------+------+-----------+
    | userid | username           | a    | b         | a    | b         |
    +--------+--------------------+------+-----------+------+-----------+
    |      1 | aaaaaaaaaaaaaaaaaa |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      2 | 螟ァ譏ッ荳ェ             |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      3 | 譏ッ蠕キ蝗ス             |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      4 |                    |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      5 | 莠懶ス難ス・            |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      6 | 螢ォ螟ァ螟ォ             |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      7 | d                  |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      8 | 螳俶婿               |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    |      9 | ?                  |    9 | 譏ッ蠕キ蝗ス    |    9 | 譏ッ蠕キ蝗ス    |
    +--------+--------------------+------+-----------+------+-----------+
    9 rows in set (0.00 sec)
    可以参考这个 写道from 后