假设我有一个设计糟糕的桌子,甚至没有。 它看起来像这样:
+-----+---------+---------+---------+----------
| id | value 1 | value 2 | value 3 | others...
+-----+---------+---------+---------+----------
| 1 | 1v1 | 1v2 | 1v3 | sth
+-----+---------+---------+---------+----------
| 2 | 2v1 | 2v2 | 2v3 | sth
+-----+---------+---------+---------+----------
现在我想从此表中选择值1,值2,值3并将其显示在列中。 这是预期的输出:
+----+--------+
| id | val |
+----+--------+
| 1 | 1v1 |
+----+--------+
| 1 | 1v2 |
+----+--------+
| 1 | 1v3 |
+----+--------+
| 2 | 2v1 |
+----+--------+
| 2 | 2v2 |
+----+--------+
| 2 | 2v3 |
+----+--------+
任何想法怎么做?
答案 0 :(得分:4)
SELECT id, `value 1` AS val
FROM your_table
UNION ALL
SELECT id, `value 2` AS val
FROM your_table
UNION ALL
...
依此类推所有必要的列。您可能希望使用UNION
而不是UNION ALL
。它会删除重复项。