如何用不同的ID爆炸行?

时间:2017-04-17 13:30:00

标签: mysql explode

我需要帮助。我有qurum表

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将是我想说的最好的

1 个答案:

答案 0 :(得分:0)

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