我有一个具有以下结构的表:
+-----+------------+------------+------------+------------+
| ID | FEATURE1 | FEATURE2 | FEATURE3 | FEATURE4 |
+-----+------------+------------+------------+------------+
| 1 | red | blue | black | grey |
| 2 | yellow | | | |
| 3 | orange | red | green | golden |
| 4 | orange | grey | blue | |
我想要查询表并返回所有可能的功能,以便在自动完成调用中使用。我需要查询只返回唯一值。
SELECT (FEATURE1, FEATURE2, FEATURE3, FEATURE4, FEATURE5) FEATURE
FROM products
WHERE `FEATURE` LIKE 'gr%\'
我想要的输出是:
+----------+
| FEATURE |
+----------+
| green |
| grey |
答案 0 :(得分:4)
基本上,您可以使用UNION
将所有列组合在一行中。
SELECT *
FROM
(
SELECT FEATURE1 FEATURE FROM TableName
UNION
SELECT FEATURE2 FEATURE FROM TableName
UNION
SELECT FEATURE3 FEATURE FROM TableName
UNION
SELECT FEATURE4 FEATURE FROM TableName
) sub
WHERE FEATURE LIKE 'gr%'