2011-09-13 266 views
0

我遇到了子查詢問題。此查詢顯示我需要進行查詢的功能,我需要MYSQL子查詢選擇

SELECT *, 
open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom, 
open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo, 
GROUP_CONCAT(open_hour_day) AS days 
FROM `open_hours` WHERE open_hour_connect_id = 2 
GROUP BY timeFrom, timeTo 
ORDER BY days 

這兩個函數

open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom, 
open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo, 

我知道,子查詢可以返回只有一個值。但我怎樣才能使用timeFrom和timeTo變量?我應該把HAVING放進去,我該怎麼做?

SELECT *, 
    (SELECT GROUP_CONCAT(open_hour_day) 
     FROM ` WHERE open_hour_connect_id = 2 
     GROUP BY timeFrom, timeTo ORDER BY days) 
FROM connections 
+0

我編輯您的文章,因此第二個查詢是可讀的,但後來我注意到,子查詢的FROM子句是不完整的。那是打算? –

+0

是的,這是有意的,因爲我不知道我在哪裏可以放兩個功能。但我想我得到了我的答案。 – Luka

回答

0

我猜你真正想要的財產以後像

SELECT c.*, sub.* 
FROM 
    connections c 
    INNER JOIN 
    ( 
SELECT *, 
    open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom, 
    open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo, 
    GROUP_CONCAT(open_hour_day) AS days 
    FROM `open_hours` WHERE open_hour_connect_id = 2 
    GROUP BY timeFrom, timeTo 
    ORDER BY days) sub 
ON c.days = sub.days