作为单个mysql查询执行两个不同的查询

时间:2019-01-08 06:40:56

标签: mysqli

$query1 = "SELECT 
    SUM(dischargecount) AS dis, MONTHNAME(dischargedate) AS n
FROM
    user
WHERE
    DATE(dischargedate) BETWEEN '$from' AND '$to'
GROUP BY MONTH(dischargedate)";


$query2 = "SELECT 
    COUNT(DISTINCT response_user) AS val1,
    MONTHNAME(response_on) AS m
FROM
    response
WHERE
    response_on BETWEEN '$from' AND '$to'
GROUP BY MONTH(response_on)";

我想将两个查询作为一个执行。

2 个答案:

答案 0 :(得分:0)

从$ query1 $ q1,$ query2 $ q2中选择col_name $ q1,col_name $ q2,其中condition1和condition2;

答案 1 :(得分:0)

SELECT 
    SUM(dischargecount) AS dis, MONTHNAME(dischargedate) AS n
FROM
    user
WHERE
    DATE(dischargedate) BETWEEN '$from' AND '$to'
GROUP BY MONTH(dischargedate)
UNION ALL
SELECT 
    COUNT(DISTINCT response_user) AS val1,
    MONTHNAME(response_on) AS m
FROM
    response
WHERE
    response_on BETWEEN '$from' AND '$to'
GROUP BY MONTH(response_on)