加入5个mysql查询

时间:2014-01-08 12:35:59

标签: mysql join mysql-logic

您好我有5个以上的mysql查询,我希望通过执行单个查询来查看所有查询的输出 请让我知道如何加入所有这些疑问。

1

select sum(msu) 
from wgr_raw 
where dt 
between '2013-11-01' and '2013-11-07';

2

select * 
from wgr_raw 
where dt 
between '2013-11-01' and '2013-11-07' 
order by dt;  

3。

select admin,sum(msu) 
from wgr_raw 
where dt 
between '2013-11-01' and '2013-11-07' 
group by admin;  

4。

SELECT admin, sum(msu) 
from wgr_raw 
where dt 
between '2013-11-01' and '2013-11-07' 
group by admin 
order by msu desc 
limit 25; 

Select ADMIN1,WEEK1,WEEK2,WEEK3,
       (((WEEK3-WEEK2)/WEEK2)*100) as percentage_change, 
         (WEEK3-WEEK2) as MSU_Difference 
from 
       ((select admin as ADMIN1, sum(msu) as WEEK1 
         from wgr_raw 
         where dt >= date_sub(date('2013-11-01'), 
             INTERVAL 25 DAY) and dt < date_sub(date('2013-11-07'), 
             INTERVAL 18 DAY) 
         group by admin 
         order by WEEK1) as q1,
       (select admin as ADMIN2, sum(msu) as WEEK2 
        from wgr_raw 
        where dt >= date_sub(date('2013-11-01'), 
             INTERVAL 18 DAY) and dt < date_sub(date('2013-11-07'), 
             INTERVAL 11 DAY) 
         group by admin 
         order by WEEK2) as q2,
       (select admin as ADMIN3, sum(msu) as WEEK3 
        from wgr_raw where dt >= date_sub(date('2013-11-01'), 
             INTERVAL 11 DAY) and dt < date_sub(date('2013-11-07'), 
             INTERVAL 4 DAY) 
         group by admin 
         order by WEEK3) as q3)  
where ADMIN1=ADMIN3 and ADMIN2=ADMIN3 
group by admin1 
order by WEEK1 desc 
limit 25;  

5。

select dt,sum(msu) 
from wgr_raw 
where dt >= date_sub(date('2013-11-01'), 
   INTERVAL 11 DAY) 
group by dt 
limit 7;  

1 个答案:

答案 0 :(得分:1)

您可以使用UNION合并n个选择查询,但UNION中有一个条件。您的所有选择字段应该相同,并且排序依据。

(
    (SELECT a, b FROM tbl WHERE id >= 50 and id <= 100)
    UNION
    (SELECT a, b FROM tbl WHERE id >= 150 and id <= 200)
    UNION
    (SELECT a, b FROM tbl WHERE id >= 250 and id <= 300)
    UNION
    (SELECT a, b FROM tbl WHERE id >= 350 and id <= 400)

)
ORDER BY a