Ashes
Ashes

Reputation: 3

MySQL selection in one column

We have a following table:

+-------+----------+----------+
| id    | time1    | time2    |
+-------+----------+----------+
| 18413 | 00:00:00 | 00:00:24 |
| 18415 | 00:00:01 | 00:00:25 |
| 18557 | 00:00:02 | 00:01:31 |
| 18432 | 00:00:03 | 00:00:30 |
| 18428 | 00:00:04 | 00:00:28 |
+-------+----------+----------+

How can we make this one using mysql selection:

+-------+----------+
| time    | result |
+-------+----------+
| 00:00:00 | time1 |
| 00:00:01 | time1 |
| 00:00:02 | time1 |
| 00:00:03 | time1 |
| 00:00:04 | time1 |
| 00:00:24 | time2 |
+-------+----------+

We mix time1 and time2 in one column and group them by type and in the result column we mark them if they were time1 or time2.

Upvotes: 0

Views: 65

Answers (2)

Raging Bull
Raging Bull

Reputation: 18767

Use a UNION:

SELECT  time1 as time,'time 1' as result
FROM TableName
UNION ALL
SELECT time2,'time 2' as result
FROM TableName

See an example in Fiddle.

EDIT:

If you want to order them as per the time column, just add a ORDER BY clause:

SELECT  time1 as time,'time 1' as result
FROM TableName
UNION ALL
SELECT time2,'time 2' as result
FROM TableName
ORDER BY T.time

Upvotes: 3

Michał Szydłowski
Michał Szydłowski

Reputation: 3419

Create a new table with the fields you specified, and first insert into it values (SELECT time1 FROM first_tabl), "time1", and then similarily insert for "time2" column.

Upvotes: 0

Related Questions