我需要选择两个字段,但条件不同,我尝试了以下操作:

SELECT
(select my_field from my_table where another_field=1) as column_one,
(select my_field from my_table where another_field=2) as column_two;


但是我得到了这个错误:

Subquery returns more than 1 row

有没有办法使这项工作?

最佳答案

在当前形式下,您可以通过添加limit子句使其工作,如下所示:

SELECT
(select my_field from my_table where another_field=1 limit 1) as column_one,
(select my_field from my_table where another_field=2 limit 1) as column_two;


但是,从逻辑上讲,这没有任何意义,因为没有附带limitORDER BY子句不能保证每次查询运行时都有特定的结果。

10-06 13:38
查看更多