简体   繁体   中英

Getting 1 column out of Three columns from Subquery

In my 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

I want it to return the Subquery values separated by commas like that

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

But i keep getting Operand should contain 1 column(s) , How can I deal with this problem overall when I want to return multiple column values in a single column from a Subquery?

What I get is the error I mentioned but what i want to get is

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

I believe you want 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM