MYSQL if a select query returns 0 rows then another select query?

前端 未结 7 1140
故里飘歌
故里飘歌 2020-12-03 18:01

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 si

相关标签:
7条回答
  • 2020-12-03 18:15

    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

    0 讨论(0)
  • 2020-12-03 18:18

    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.

    0 讨论(0)
  • 2020-12-03 18:26

    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);
    
    0 讨论(0)
  • 2020-12-03 18:30

    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.

    0 讨论(0)
  • 2020-12-03 18:34

    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.

    0 讨论(0)
  • 2020-12-03 18:34

    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.

    0 讨论(0)
提交回复
热议问题