从Subquery的三列中获取1列

时间:2018-02-15 19:56:40

标签: mysql sql

在我的Query

SELECT a.id, a.rev, a.content,
(
  SELECT 
  EXISTS(SELECT id FROM `docs` WHERE id = 1), 
  EXISTS(SELECT id FROM `docs` WHERE id = 2), 
  EXISTS(SELECT id FROM `docs` WHERE id = 3)
) AS ex
FROM `docs` a

我希望它返回由逗号分隔的子查询值,如

id rev content ex
1  1   ....... 1,1,0

但是我一直得到Operand should contain 1 column(s),当我想从子查询中返回单个列中的多个列值时,我怎样才能解决这个问题呢?

我得到的是我提到的error,但我想得到的是

id  rev     content                                              ex
1   1       The earth is flat                                    1,1,0
1   2       The earth is flat and rests on a bull's horn         1,1,0
1   3       The earth is like a ball.                            1,1,0
2   1       One hundred angels can dance on the head of a pin    1,1,0

1 个答案:

答案 0 :(得分:2)

我相信你想要CONCAT

Fiddle example

SELECT a.id, a.rev, a.content,
       CONCAT(CAST(EXISTS(SELECT id FROM `docs` WHERE id = 1) as char(1)),',',
              CAST(EXISTS(SELECT id FROM `docs` WHERE id = 2) as char(1)),',',
              CAST(EXISTS(SELECT id FROM `docs` WHERE id = 3) as char(1))
       ) AS ex
FROM `docs` a