首页 > 数据库技术 > 详细

MySQL: Lasy UNION

时间:2015-06-15 18:12:03      阅读:165      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/binglong/p/4578483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!