mysql query to display row data in one column

76 views Asked by At

I wrote query like

SELECT 
SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 1) AS level1,
IF(@num_lines > 1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 2), ',', -1), '') AS level2,
IF(@num_lines > 2, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 3), ',', -1), '') AS level3,
IF(@num_lines > 3, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 4), ',', -1), '') AS level4,
IF(@num_lines > 4, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 5), ',', -1), '') AS level5
FROM hrm_t_interview inter, 
(SELECT  @num_lines := 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', ''))  FROM hrm_t_interview WHERE INT_APPLICANTID=15) temp
WHERE inter.INT_APPLICANTID=15   

I displayed values like

 level1    |   level2     |   level3
======================================
   4       |    3         |    5

I want to display values like

 column1  |   column2
========================
  level1  |     4
  level2  |     3
  level3  |     5

Please help me using mysql.

1

There are 1 answers

0
Kickstart On BEST ANSWER

Crude way would be to use multiple unioned queries, something like this:-

SELECT 'level1' AS column1, SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT INT_APPLICANTID, 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
    HAVING num_lines > 0
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
WHERE inter.INT_APPLICANTID=15
UNION
SELECT 'level2' AS column1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 2), ',', -1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT INT_APPLICANTID, 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
    HAVING num_lines > 1
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
WHERE inter.INT_APPLICANTID=15
UNION
SELECT 'level3' AS column1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 3), ',', -1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT INT_APPLICANTID, 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
    HAVING num_lines > 2
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
WHERE inter.INT_APPLICANTID=15
UNION
SELECT 'level4' AS column1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 4), ',', -1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT INT_APPLICANTID, 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
    HAVING num_lines > 3
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
WHERE inter.INT_APPLICANTID=15
UNION
SELECT 'level5' AS column1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', 5), ',', -1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT INT_APPLICANTID, 1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
    HAVING num_lines > 4
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
WHERE inter.INT_APPLICANTID=15

A bit more elegant might be to generate a range of rows, one for each possible delimited value.

Not tested but something like this:-

SELECT CONCAT('level', temp2.iCnt) AS column1, SUBSTRING_INDEX(SUBSTRING_INDEX(inter.CHR_SKILLLEVELS, ',', temp2.iCnt), ',', -1) AS column2
FROM hrm_t_interview inter
INNER JOIN
(
    SELECT  1 + LENGTH(CHR_SKILLLEVELS) - LENGTH(REPLACE(CHR_SKILLLEVELS, ',', '')) AS num_lines
    FROM hrm_t_interview 
    GROUP BY INT_APPLICANTID
) temp
ON inter.INT_APPLICANTID = temp.INT_APPLICANTID
INNER JOIN
(
    SELECT 0 AS iCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
) temp2
ON temp.num_lines >= temp2.iCnt
WHERE inter.INT_APPLICANTID=15

If you can paste up some test data I will check the sql.