MYSQL 如果一个选择查询返回 0 行,那么另一个选择查询?
如果 select * from table where x=1
返回 0 行,那么我需要 select * from table where x=2 [或其他一些查询]
。是否可以使用条件语句在单个 MySQL 查询中执行此操作?
编辑:所有使用 UNION
的答案都有效,但前提是两个查询都从同一个表(或具有相同列数的表)中进行选择。如果第二个查询应用到具有联接的不同表上怎么办?
让我写下我的查询以使问题更清楚:
第一:
SELECT table1.a, table2.b from table1 LEFT JOIN table2 ON table2.x= table1.x
WHERE .....
如果第一个查询的结果为空,那么:
第二:
SELECT table1.a FROM table1
WHERE ....
如果第一个查询返回任何结果,我将使用第一个查询中的行,否则第二个查询将是用过的。
if select * from table where x=1
returns 0 rows, then I need select * from table where x=2 [or some other query]
. Is it possible to do this in a single MySQL query with a conditional statement?
Edit: All answers with UNION
work, but only if both queries select from the same table (or tables with the same number of columns). What if the second query is applied on a different table with joins?
Let me write down the my queries to make the question more clear:
1st:
SELECT table1.a, table2.b from table1 LEFT JOIN table2 ON table2.x= table1.x
WHERE .....
if the result from the 1st one is null then:
2nd:
SELECT table1.a FROM table1
WHERE ....
I will be using the rows from the 1st query if it returns any, otherwise the 2nd one will be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这似乎是我刚刚进行的快速测试的结果,并且避免了两次检查
x=1
是否存在的需要。编辑:在您对问题进行澄清之后,显然这两个查询需要与 UNION 兼容才能使上述工作正常进行。
您更新的问题的答案是否定的。这在单个查询中是不可能的。您需要使用一些条件过程逻辑来执行所需的查询。
This appears to work from a quick test I just did and avoids the need to check for the existence of
x=1
twice.Edit: Following your clarification to the question obviously the 2 queries will need to be UNION compatible for the above to work.
The answer to your updated question is No. This is not possible in a single query. You would need to use some conditional procedural logic to execute the desired query.
你可以尝试...
如果你不认为这是一个太可怕的黑客行为。
You could try...
if you don't consider it too ghastly a hack.
SQL_CALC_FOUND_ROWS
和FOUND_ROWS
不能在单个查询中使用,即使由UNION
语句分隔也是如此。正确的方法是:
使用
JOIN
和UNION ALL
,此查询的性能几乎等同于任一单独的独立查询SQL_CALC_FOUND_ROWS
andFOUND_ROWS
cannot be used in a single query, even if separate byUNION
statements.The correct way to do this would be:
With the
JOIN
and theUNION ALL
the performance of this query is almost equivalent to either of the individual standalone queriesyes
存在或不存在的子查询
http ://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-subqueries.html
示例:
yes
Subqueries with EXISTS or NOT EXISTS
http://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-subqueries.html
example :
如果两个查询返回的列数不同,您可以用空列填充其中一个结果,并首先添加标识符列。
其中 mytable2 比 mytable 多 4 列。
If the two queries return different number of columns, you can pad one of the results with empty columns and also add an identifier column first.
Where mytable2 has 4 more columns than mytable.
最简单的解释是:
“if”语句为选定的值提供了一些功能。
结构是这样的:
这里可以找到很好的解释。
The simplest explanation is that:
The 'if' statement give some functionality to selected values.
The structure is something like this:
A great explanation can be found here.
您可以使用 EXIST 和 NOT EXIST 语句来检查结果是否为空。如果结果为 Null,那么您可以从 table2 中获取值。
you can use EXIST and NOT EXIST statement to check that result is null or not. if result is Null then you can get value from table2.