查询两个表的数据 union

来源:互联网 发布:制作mod的软件 编辑:程序博客网 时间:2024/05/16 10:08

注意,两张表的字段名最好保持相同


查询两张表的数据,包含相同的数据:


    select         id      from tab_a  where  a='1' 
    
    union ALL 
    
    select     id       from tab_b  where  a='1' 
 
    order by time  desc    limit   0,10


查询两张表的数据,去掉相同的数据:


    select         id      from tab_a  where  a='1' 
    
    union
    
    select     id       from tab_b  where  a='1' 
 
    order by time  desc    limit   0,10


查询两张表合并后的总条数:



SELECT SUM(aa) from(  

           select     COUNT(*)     as     aa

   
    from tab_a  where  id='2'


     union ALL 
    
    select     COUNT(*)    as     aa
    
    from tab_b
  
    where  id='2' )tab_c  
0 0
原创粉丝点击