0

我知道这张桌子不是它应该的样子,但它就是它,它不能改变。

我的表:

|carID  | car1      | car2      | car3      |
-------------------------------------------------
|   1   | someCar 1 | someCar 2 | someCar 3 |
-------------------------------------------------

我想要以下结果,就像它应该在关系表中一样:

|carID  |car        |
---------------------
|1      | someCar 1 |
|1      | someCar 2 |
|1      | someCar 3 |

有人知道该怎么做吗?

我试过这样的事情:

SELECT carId, car1, car2, car3 FROM cars WHERE carId = '1' GROUP BY car1, car2, car3
4

3 回答 3

2

UNION

SELECT   CarID, car1 car FROM MyTable
UNION ALL
SELECT   CarID, car2 car FROM MyTable
UNION ALL
SELECT   CarID, car3 car FROM MyTable
于 2013-05-27T07:44:27.993 回答
0

尝试UNION ALL

SELECT CarID, Car1 FROM myTable WHERE carID='1'
UNION ALL
SELECT CarID, Car2 FROM myTable WHERE carID='1'
UNION ALL
SELECT CarID, Car3 FROM myTable WHERE carID='1'
于 2013-05-27T07:44:37.150 回答
0

创建三个选择的并集:

SELECT carID, car1 FROM cars WHERE carID = '1'
UNION ALL
SELECT carID, car2 FROM cars WHERE carID = '1'
UNION ALL
SELECT carID, car3 FROM cars WHERE carID = '1'

请注意,您需要使用UNION ALL,而不仅仅是UNION,因为UNION单独使用会遗漏重复项。例如,如果car2car3相同,则结果将仅包含两条记录。

于 2013-05-27T07:44:57.887 回答