加入四个具有相同列,不同条件的MySQL查询

时间:2013-06-18 17:30:41

标签: mysql left-join alias where-clause

我有四个查询,每个查询有6列。每个查询都是相同的,除了WHERE子句在每种情况下略有不同。我希望看到的是每个查询的每个查询的结果,以便进行比较。

示例结果表标题:time(只有一个),calls1,calls2,calls3,calls4,work1,work2,work3,work4,tele1,tele2,tele3,tele4,comm1,comm2,comm3,comm4,techs1,techs2, techs3,techs4。

实际查询如下。请帮我做一个比较查询。 Ť

SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS r_calls, ROUND(AVG(work_time),2)/60 AS r_work, ROUND(AVG(tele_time),2)/60 AS r_tele, ROUND(AVG(comm_time),2)/60 AS r_comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS r_techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15) 


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
AND call_origins_uid = 1 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15)


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
AND call_origins_uid = 4 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15)


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15)

1 个答案:

答案 0 :(得分:1)

您可以为每个表使用'CREATE TEMPORARY TABLE TableName1',然后编写一个查询以按顺序放置列,然后在完成后删除表。