MySQL 多选,多 Where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15893090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:16:18  来源:igfitidea点击:

MySQL Multiple Select, Multiple Where Clauses

mysqlselect

提问by user1114864

Instead of running hundreds of SELECT queries for one operation, I want to just run one big query, which I'm hoping will ease the load on my server.

我不想为一项操作运行数百个 SELECT 查询,而是只想运行一个大查询,我希望这会减轻我服务器上的负载。

SELECT (
(SELECT link_type_id FROM connections WHERE (node_to_id = '0' AND node_from_id = '1') OR (node_from_id = '1' AND node_to_id = '0')),
(SELECT link_type_id FROM connections WHERE (node_to_id = '0' AND node_from_id = '2') OR (node_from_id = '2' AND node_to_id = '0'))
)

There will be many more SELECTS in this query, but even the two aren't working. When I run this code I get the error:

此查询中将有更多 SELECTS,但即使这两个也不起作用。当我运行此代码时,出现错误:

Operand should contain 1 column(s).

Any suggestions? Thanks!

有什么建议?谢谢!

回答by shola

You can try below may be but you may need UNION

您可以尝试以下可能但您可能需要 UNION

SELECT link_type_id FROM connections 
WHERE (node_to_id = '0' AND node_from_id = '1') 
OR (node_from_id = '1' AND node_to_id = '0')
UNION
SELECT link_type_id FROM connections 
WHERE (node_to_id = '0' AND node_from_id = '2') 
OR (node_from_id = '2' AND node_to_id = '0')

回答by Explosion Pills

This is caused by the parentheses outside of the columns. Even something as simple as:

这是由列外的括号引起的。甚至像这样简单的事情:

SELECT ((SELECT 1), (SELECT 1))

Will yield this error -- the problem is that MySQL can only display one column per ... well ... column, and the entire ()wraps a single column. If you remove the outer parentheses it will display each SELECTin a separate column.

会产生这个错误——问题是 MySQL 只能显示每一列......好吧......列,并且整个()包装单列。如果删除外括号,它将SELECT在单独的列中显示每个括号。