2017-04-17 46 views
0

我需要幫助。我的Qurum表如何用不同的ID來爆炸行?

qurum table    data table 
qurum_id|qurum   id|qurum1 
     1|one    1|3 
     2|two    2|2 
     3|three    3|1 
     4|four    4|7 
     5|five    5|5,6 
     6|six    6|4 
     7|seven    7|5 
where qurum_id = data.qurum1 

的結果必須是

qurum_id|qurum|qurum1 
     1|one |3 
     2|two |2 
     3|three|1 
     4|four |7 
     5|five |5 
    **6|six |6 
     6|six |4** 
     7|seven|5 

這是查詢

`SELECT qurum_id, qurum,qurum1, 
     SUM(CASE WHEN DATE_FORMAT(aa.input_date, '%m') BETWEEN 01 AND 06 
      THEN 1 END) AS I, 
     SUM(CASE WHEN DATE_FORMAT(aa.input_date, '%m') BETWEEN 07 AND 12 
      THEN 1 END) AS II 
FROM qurum AS qur 
INNER JOIN (SELECT id, qurum1,input_date FROM DATA) AS aa 
      ON qur.qurum_id = aa.qurum1 
WHERE DATE_FORMAT(aa.input_date, '%Y') = $year 
GROUP BY qurum_id 
ORDER BY qurum_id` 

結果是here

有第六行與qurum1 = 6 | 7在表中,但我需要爆炸6 | 7,因爲qurum_id 6和qurum_id 7有不同的qurum。 PIC將show盡我想說

回答

0
SELECT SUBSTRING_INDEX(qurum1, ' ', 1) AS first_part, 
SUBSTRING_INDEX(qurum1, ' ', -1) as second_part FROM qurum; 
+0

什麼'選擇qurum_id,Qurum的,qurum1, SUM(CASE WHEN DATE_FORMAT(aa.input_date, '%M')之間的01和06 \t \t THEN 1 END )AS I, SUM(CASE WHEN DATE_FORMAT(aa.input_date, '%M')之間的07和12 \t \t THEN 1 END)AS II, SUBSTRING_INDEX(qurum1, '|',1)AS first_part, SUBSTRING_INDEX (qurum1,'|',-1)as second_part FROM qurum AS qur INNER JOIN(SELECT id,qurum1,input_date FROM DATA)AS aa \t \t \t ON qur.qurum_id = aa.qurum1 WHERE DATE_FORMAT(aa.input_date, '%Y')= 2017 GROUP BY qurum_id ORDER BY qurum_id'喜歡這個? –

+0

http://chat.stackoverflow.com/rooms/141896/mysql-explode –

+0

我增加了一個數字qurum1(現在它是1 | 6 | 7)和行消失 –