MySQL: Lasy UNION

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);

  

posted @ 2015-06-15 16:07  rongbin  阅读(147)  评论(0编辑  收藏  举报