当前位置:Gxlcms > 数据库问题 > MySQL: Lasy UNION

MySQL: Lasy UNION

时间:2021-07-01 10:21:17 帮助过:2人阅读

Method 1:

From book <高性能MySQL>

select greatest(@found := -1, user_id) as user_id, ‘user‘ as which_tbl
   from user where user_id = 1
union all
select user_id as user_id, ‘user_archive‘ as which_tbl
   from user_archive where user_id = 1 and @found is null
union all
select 1, ‘‘ from dual where ( @found := null ) is not null;

Method 2: I voted this....

http://stackoverflow.com/questions/3202242/mysql-if-a-select-query-returns-0-rows-then-another-select-query

SELECT SQL_CALC_FOUND_ROWS *
FROM mytable
WHERE x = 1

UNION ALL

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

  Method 3:

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

  

MySQL: Lasy UNION

标签:

人气教程排行