2015-10-15 175 views
-1

我試圖獲得更多的列從總結2頁不同的表多個查詢相同的表,但在不同的列的mysql

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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_hos group by DAY_IN )d 

,當然這是行不通的結果,只是第一個顯示的列(ARR)而其他人顯示錯誤的輸出。

我在哪裏錯了?

+1

[SELECT \ * FROM multiple tables are possible duplicate。 MySQL](http://stackoverflow.com/questions/12890071/select-from-multiple-tables-mysql)...你需要將這些表連接在一起。 –

+0

您的表格e,a,b,c,d不會*彼此連接 –

回答

0

在SQL Server 2005+中,可以使用Window函數。 但它在MySQL中不受支持。

在子查詢中,選擇語句必須具有連接運算符。就像這樣:

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

這是一個很常見的模式:

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 [email protected]_hos 
GROUP BY DAY_IN 
0

試試這個: -

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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_hos) d 

你的問題中提到有關加入2個表,但似乎只有t_hospital在題。您可能想要更新有關其他表格的信息的問題。

0

如果我正確理解你,這就是你想要的。

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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_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 [email protected]_hos) AS 'para' 
0

我很好奇你的錯誤。它會幫助別人給你準確的解決方案。你的查詢是好的,它應該返回多行,因爲你做交叉連接,所以如果第一部分[(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 [email protected]_hos group by DAY_IN )e, ]返回4並且讓別人返回3,5,6那麼你將得到4 * 5 * 6 * 3 = 360行我想你不想那樣。爲了得到一個精確的答案,在phpmyAdmin/sqlyog打印屏幕上運行select查詢的每個部分,並將其粘貼到你想要的SO中。