SQL生成两个sparate查询的子查询

时间:2016-11-25 12:32:49

标签: mysql

$sql = "SELECT max( `reg_date` ) AS endtime, min( `reg_date` ) AS starttime, sum( `distance` ) AS total FROM `user_distance` WHERE user_id =8 GROUP BY CAST( `reg_date` AS DATE )";

$sql1="SELECT CAST( `reg_date` AS DATE ) AS date, count( * ) AS customer_total FROM `customer` WHERE user_id =8 GROUP BY CAST( `reg_date` AS DATE )";

我正在为这两个查询获得完美的结果,但我想结合这两个查询的结果。

1 个答案:

答案 0 :(得分:0)

drop temporary table if exist temp1;
create temporary table temp1
SELECT max( `reg_date` ) AS endtime, min( `reg_date` ) AS starttime, sum( `distance` ) AS total 
FROM `user_distance` 
WHERE user_id =8 GROUP BY CAST( `reg_date` AS DATE )"

union 

SELECT CAST( `reg_date` AS DATE ) AS date,''as starttime, count( * ) AS customer_total 
FROM `customer` 
WHERE user_id =8 GROUP BY CAST( `reg_date` AS DATE )";

select * from temp1;