MYSQL 如果一个选择查询返回 0 行,那么另一个选择查询?

发布于 2024-09-08 14:32:26 字数 566 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

各自安好 2024-09-15 14:32:26

这似乎是我刚刚进行的快速测试的结果,并且避免了两次检查 x=1 是否存在的需要。

SELECT SQL_CALC_FOUND_ROWS *
FROM mytable
WHERE x = 1

UNION ALL

SELECT *
FROM mytable
WHERE 
FOUND_ROWS() = 0 AND x = 2;

编辑:在您对问题进行澄清之后,显然这两个查询需要与 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.

SELECT SQL_CALC_FOUND_ROWS *
FROM mytable
WHERE x = 1

UNION ALL

SELECT *
FROM mytable
WHERE 
FOUND_ROWS() = 0 AND x = 2;

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.

〃温暖了心ぐ 2024-09-15 14:32:26

你可以尝试...

SELECT *
    FROM mytable
    WHERE x = 1

UNION

SELECT *
    FROM mytable
    WHERE x = 2 AND
          NOT EXISTS (SELECT *
                          FROM mytable
                          WHERE x = 1);

如果你不认为这是一个太可怕的黑客行为。

You could try...

SELECT *
    FROM mytable
    WHERE x = 1

UNION

SELECT *
    FROM mytable
    WHERE x = 2 AND
          NOT EXISTS (SELECT *
                          FROM mytable
                          WHERE x = 1);

if you don't consider it too ghastly a hack.

东北女汉子 2024-09-15 14:32:26

SQL_CALC_FOUND_ROWSFOUND_ROWS 不能在单个查询中使用,即使由 UNION 语句分隔也是如此。

正确的方法是:

WITH  my_cte AS
(
  SELECT * from original_set
)
SELECT * FROM my_cte
UNION ALL
SELECT opt.* FROM optional_set opt JOIN (SELECT count(*) v FROM my_cte) count ON count.v=0;

使用 JOINUNION ALL,此查询的性能几乎等同于任一单独的独立查询

SQL_CALC_FOUND_ROWS and FOUND_ROWS cannot be used in a single query, even if separate by UNION statements.

The correct way to do this would be:

WITH  my_cte AS
(
  SELECT * from original_set
)
SELECT * FROM my_cte
UNION ALL
SELECT opt.* FROM optional_set opt JOIN (SELECT count(*) v FROM my_cte) count ON count.v=0;

With the JOIN and the UNION ALL the performance of this query is almost equivalent to either of the individual standalone queries

从﹋此江山别 2024-09-15 14:32:26

yes

存在或不存在的子查询

http ://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-subqueries.html

示例:

SELECT column1 FROM t1 WHERE NOT EXISTS (SELECT * FROM t2);

yes

Subqueries with EXISTS or NOT EXISTS

http://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-subqueries.html

example :

SELECT column1 FROM t1 WHERE NOT EXISTS (SELECT * FROM t2);
真心难拥有 2024-09-15 14:32:26

如果两个查询返回的列数不同,您可以用空列填充其中一个结果,并首先添加标识符列。

SELECT SQL_CALC_FOUND_ROWS 1 query_type, mytable.*, 
'' col1, '' col2, '' col3, '' col4
FROM mytable
WHERE x = 1

UNION ALL

SELECT 2, mytable2.*
FROM mytable2
WHERE 
FOUND_ROWS() = 0 AND x = 2;

其中 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.

SELECT SQL_CALC_FOUND_ROWS 1 query_type, mytable.*, 
'' col1, '' col2, '' col3, '' col4
FROM mytable
WHERE x = 1

UNION ALL

SELECT 2, mytable2.*
FROM mytable2
WHERE 
FOUND_ROWS() = 0 AND x = 2;

Where mytable2 has 4 more columns than mytable.

川水往事 2024-09-15 14:32:26

最简单的解释是:

SELECT IF(1 = 2,'true','false'); --> false
SELECT IF(1 = 1,' true','false'); --> true
SELECT IF(1 = 2,' true','false'), IF(1 = 1,' true','false'); --> false | true

“if”语句为选定的值提供了一些功能。
结构是这样的:

SELECT IF(<your statement>), ...<selected params>... FROM <your tables>

这里可以找到很好的解释。

The simplest explanation is that:

SELECT IF(1 = 2,'true','false'); --> false
SELECT IF(1 = 1,' true','false'); --> true
SELECT IF(1 = 2,' true','false'), IF(1 = 1,' true','false'); --> false | true

The 'if' statement give some functionality to selected values.
The structure is something like this:

SELECT IF(<your statement>), ...<selected params>... FROM <your tables>

A great explanation can be found here.

画中仙 2024-09-15 14:32:26

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文