Skip to content
Advertisement

How to perform mysql query with goal of returning 2 rows when a single record is found in

SELECT DISTINCT '12345'  AS AlphabetID,
                 CASE
                    WHEN letter='a' THEN 'a'
                    END AS letter

This will return one row with a result of:

 AlphabetID | letter
 | 12345    | a

How would it be possible to return multiple rows for a given column while retaining the other column field values.

Essentially, adding rows on the fly when the conditions are met.

My thought process attempted:

SELECT DISTINCT '12345'  AS AlphabetID,
                 CASE
                    WHEN letter='a' THEN 'a'
                    WHEN letter='a' THEN 'ax'
                    END AS letter

Desired Result:

 AlphabetID | letter
 | 12345    | a
 | 12345    | ax

Advertisement

Answer

Are you looking for union all?

SELECT DISTINCT '12345'  AS AlphabetID, 'a' as letter
FROM e
WHERE E.Chem_ID_Login = 'a'
UNION ALL
SELECT DISTINCT '12345'  AS AlphabetID, 'ax' as letter
FROM e
WHERE E.Chem_ID_Login = 'a';

Or a CROSS JOIN:

SELECT DISTINCT '12345'  AS AlphabetID, l.letter
FROM e CROSS JOIN
     (SELECT 'a' as letter UNION ALL SELECT 'ae') l
WHERE E.Chem_ID_Login = 'a'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement