多个查询同一个表但在不同的列中mysql

nlejzf6q  于 2021-08-13  发布在  Java
关注(0)|答案(4)|浏览(296)

我正在尝试获取更多的列来汇总来自两个不同表的结果

SET @start_res = 20150301;
SET @finish_res= 20150501;
SET @finish_check= 20150801;
SET @start_check= 20150301;
SET @daily_hos= 3;

SELECT* from
   ( SELECT COUNT(DAY_IN) AS arr FROM t_hospital WHERE DAY_IN between  @start_check and @finish_check and RES_DATE between @start_res and @finish_res  and ID_daily_hos =@daily_hos group by DAY_IN  )e,
     (SELECT COUNT(PAT_STATUS) AS ONG1 FROM t_hospital WHERE PAT_STATUS  like '%ong%' and  DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res  and ID_daily_hos =@daily_hos group by DAY_IN ) a,
        (SELECT COUNT(PAT_STATUS) AS RTED FROM t_hospital WHERE PAT_STATUS  like '%rtde%'and  DAY_IN between  @start_check and @finish_check and RES_DATE between @start_res and @finish_res  and ID_daily_hos =@daily_hos group by DAY_IN  )b,
           (SELECT COUNT(PAT_STATUS) AS POLI FROM t_hospital WHERE PAT_STATUS  like '%pol%'and DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res  and ID_daily_hos =@daily_hos group by DAY_IN   )c,
              (SELECT COUNT(PAT_STATUS) AS para FROM t_hospital WHERE PAT_STATUS  like '%para%' and  DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res  and ID_daily_hos =@daily_hos group by DAY_IN  )d

当然,它不起作用,只有第一个显示列(arr)起作用,而其他列显示错误的输出。
我错在哪里?

q0qdq0h2

q0qdq0h21#

尝试this:-

SET @start_res = 20150301;
SET @finish_res= 20150501;
SET @finish_check= 20150801;
SET @start_check= 20150301;
SET @daily_hos= 3;

SELECT 

(SELECT COUNT(DAY_IN) AS arr FROM t_hospital WHERE 
DAY_IN between  @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res and 
ID_daily_hos =@daily_hos) e,

(SELECT COUNT(PAT_STATUS) AS ONG1 FROM t_hospital WHERE 
PAT_STATUS  like '%ong%' and  
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) a,

(SELECT COUNT(PAT_STATUS) AS RTED FROM t_hospital WHERE 
PAT_STATUS  like '%rtde%' and  
DAY_IN between  @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos)b,

(SELECT COUNT(PAT_STATUS) AS POLI FROM t_hospital WHERE 
PAT_STATUS  like '%pol%' and 
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) c,

(SELECT COUNT(PAT_STATUS) AS para FROM t_hospital WHERE 
PAT_STATUS  like '%para%' and  
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) d

您的问题提到要连接两个表,但似乎只有 t_hospital 有问题。您可能想用另一个表的信息更新您的问题。

i7uq4tfw

i7uq4tfw2#

如果我没听错的话,这就是你想要的。

SET @start_res = 20150301;
SET @finish_res= 20150501;
SET @finish_check= 20150801;
SET @start_check= 20150301;
SET @daily_hos= 3;

SELECT 

(SELECT COUNT(DAY_IN) FROM t_hospital WHERE 
DAY_IN between  @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res and 
ID_daily_hos =@daily_hos) AS 'arr' ,

(SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
PAT_STATUS  like '%ong%' and  
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) AS 'ONG1' ,

(SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
PAT_STATUS  like '%rtde%' and  
DAY_IN between  @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) AS 'RTED' ,

(SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
PAT_STATUS  like '%pol%' and 
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) AS 'POLI' ,

(SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
PAT_STATUS  like '%para%' and  
DAY_IN between @start_check and @finish_check and 
RES_DATE between @start_res and @finish_res  and 
ID_daily_hos =@daily_hos) AS 'para'
lfapxunr

lfapxunr3#

这是一个非常常见的模式:

SELECT DAY_IN, COUNT(*) AS arr,
        SUM(IF(PAT_STATUS  like '%ong%', 1, 0)) AS ONG1,
        SUM(IF(PAT_STATUS  like '%rtde%', 1, 0)) AS RTED,
        SUM(IF(PAT_STATUS  like '%pol%', 1, 0)) AS POL1,
        SUM(IF(PAT_STATUS  like '%para%', 1, 0)) AS para
FROM t_hospital 
WHERE DAY_IN between @start_check and @finish_check 
    and RES_DATE between @start_res and @finish_res  
    and ID_daily_hos =@daily_hos
GROUP BY DAY_IN
ylamdve6

ylamdve64#

在SQLServer2005+中,可以使用窗口函数。但是mysql不支持它。
在子查询中,select语句必须具有join运算符。这样地:

SELECT * FROM (
    (SELECT COUNT(..) ...) AS C1
       INNER JOIN (SELECT COUNT(...) ...) AS C2
            ON <condition>
       INNER JOIN ... )

相关问题