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

fslejnso  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(330)

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

  1. SET @start_res = 20150301;
  2. SET @finish_res= 20150501;
  3. SET @finish_check= 20150801;
  4. SET @start_check= 20150301;
  5. SET @daily_hos= 3;
  6. SELECT* from
  7. ( 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,
  8. (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,
  9. (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,
  10. (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,
  11. (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)起作用,而其他列显示错误的输出。
我错在哪里?

oknwwptz

oknwwptz1#

尝试this:-

  1. SET @start_res = 20150301;
  2. SET @finish_res= 20150501;
  3. SET @finish_check= 20150801;
  4. SET @start_check= 20150301;
  5. SET @daily_hos= 3;
  6. SELECT
  7. (SELECT COUNT(DAY_IN) AS arr FROM t_hospital WHERE
  8. DAY_IN between @start_check and @finish_check and
  9. RES_DATE between @start_res and @finish_res and
  10. ID_daily_hos =@daily_hos) e,
  11. (SELECT COUNT(PAT_STATUS) AS ONG1 FROM t_hospital WHERE
  12. PAT_STATUS like '%ong%' and
  13. DAY_IN between @start_check and @finish_check and
  14. RES_DATE between @start_res and @finish_res and
  15. ID_daily_hos =@daily_hos) a,
  16. (SELECT COUNT(PAT_STATUS) AS RTED FROM t_hospital WHERE
  17. PAT_STATUS like '%rtde%' and
  18. DAY_IN between @start_check and @finish_check and
  19. RES_DATE between @start_res and @finish_res and
  20. ID_daily_hos =@daily_hos)b,
  21. (SELECT COUNT(PAT_STATUS) AS POLI FROM t_hospital WHERE
  22. PAT_STATUS like '%pol%' and
  23. DAY_IN between @start_check and @finish_check and
  24. RES_DATE between @start_res and @finish_res and
  25. ID_daily_hos =@daily_hos) c,
  26. (SELECT COUNT(PAT_STATUS) AS para FROM t_hospital WHERE
  27. PAT_STATUS like '%para%' and
  28. DAY_IN between @start_check and @finish_check and
  29. RES_DATE between @start_res and @finish_res and
  30. ID_daily_hos =@daily_hos) d

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

展开查看全部
7dl7o3gd

7dl7o3gd2#

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

  1. SELECT * FROM (
  2. (SELECT COUNT(..) ...) AS C1
  3. INNER JOIN (SELECT COUNT(...) ...) AS C2
  4. ON <condition>
  5. INNER JOIN ... )
wooyq4lh

wooyq4lh3#

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

  1. SET @start_res = 20150301;
  2. SET @finish_res= 20150501;
  3. SET @finish_check= 20150801;
  4. SET @start_check= 20150301;
  5. SET @daily_hos= 3;
  6. SELECT
  7. (SELECT COUNT(DAY_IN) FROM t_hospital WHERE
  8. DAY_IN between @start_check and @finish_check and
  9. RES_DATE between @start_res and @finish_res and
  10. ID_daily_hos =@daily_hos) AS 'arr' ,
  11. (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE
  12. PAT_STATUS like '%ong%' and
  13. DAY_IN between @start_check and @finish_check and
  14. RES_DATE between @start_res and @finish_res and
  15. ID_daily_hos =@daily_hos) AS 'ONG1' ,
  16. (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE
  17. PAT_STATUS like '%rtde%' and
  18. DAY_IN between @start_check and @finish_check and
  19. RES_DATE between @start_res and @finish_res and
  20. ID_daily_hos =@daily_hos) AS 'RTED' ,
  21. (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE
  22. PAT_STATUS like '%pol%' and
  23. DAY_IN between @start_check and @finish_check and
  24. RES_DATE between @start_res and @finish_res and
  25. ID_daily_hos =@daily_hos) AS 'POLI' ,
  26. (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE
  27. PAT_STATUS like '%para%' and
  28. DAY_IN between @start_check and @finish_check and
  29. RES_DATE between @start_res and @finish_res and
  30. ID_daily_hos =@daily_hos) AS 'para'
展开查看全部
rfbsl7qr

rfbsl7qr4#

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

  1. SELECT DAY_IN, COUNT(*) AS arr,
  2. SUM(IF(PAT_STATUS like '%ong%', 1, 0)) AS ONG1,
  3. SUM(IF(PAT_STATUS like '%rtde%', 1, 0)) AS RTED,
  4. SUM(IF(PAT_STATUS like '%pol%', 1, 0)) AS POL1,
  5. SUM(IF(PAT_STATUS like '%para%', 1, 0)) AS para
  6. FROM t_hospital
  7. WHERE DAY_IN between @start_check and @finish_check
  8. and RES_DATE between @start_res and @finish_res
  9. and ID_daily_hos =@daily_hos
  10. GROUP BY DAY_IN

相关问题