Is it possible for a subquery to return two values

2019-02-22 05:02发布

Is it possible for a subquery to return two values onto the outer query? Such as:

SELECT 1, 
       (SELECT COUNT(*), MAX(*) FROM test_table WHERE test=123) 
FROM another_table

Or is there a better way to do this?

4条回答
我想做一个坏孩纸
2楼-- · 2019-02-22 05:40

Hmm, it depends on what exactly you want to do with the data, you can join two tables using JOIN syntax, and one of the tables can actually be a subquery. I think that is probably what you want.

I'm not even user what your current query will do..

Documentation: http://dev.mysql.com/doc/refman/5.0/en/join.html

查看更多
淡お忘
3楼-- · 2019-02-22 05:41

You are just selecting numbers as results so couldn't you just do:

SELECT 1, COUNT(*), MAX(*) FROM test_table WHERE test=123
查看更多
倾城 Initia
4楼-- · 2019-02-22 05:54

If you use the subquery in the FROM clause rather than the field list, then you can treat the output as a table and refer to the separate columns.

查看更多
男人必须洒脱
5楼-- · 2019-02-22 05:57

Not possible

mysql> select 1, (select 2, 3) from dual;
ERROR 1241 (21000): Operand should contain 1 column(s)

If you are dealing with two tables and you what the results in one line, you should preform a join.

查看更多
登录 后发表回答