I have a table like this:
SELECT
parent_id,
parent2_id,
parent3_id,
parent4_id
FROM
parents
I need to make it like this:
I need to add column with an unique value of a row, but since we have 4 columns, then there are four rows. But, if anyone can make this result table without zero value in id column, it would be very cool.
upd: I found an answer, but it seems very large. Can the solution be simplified?
SELECT * FROM
(SELECT
parent_id as id,
parent_id,
parent2_id,
parent3_id,
parent4_id
FROM
parents
UNION ALL
SELECT parent2_id as id,
parent_id,
parent2_id,
parent3_id,
parent4_id
FROM
parents
UNION ALL
SELECT parent3_id as id,
parent_id,
parent2_id,
parent3_id,
parent4_id
FROM
parents
UNION ALL
SELECT parent4_id as id,
parent_id,
parent2_id,
parent3_id,
parent4_id
FROM
parents) as src
WHERE src.id != 0