我不擅长SQL并想知道是否可以这样做:我有两个表:table_a和table_b。两个表都有一个名为category的TEXT类型列。
示例:
TABLE_A
|-id-|-category-|
| 1 | fruits |
| 2 | meats |
| 3 | fruits |
| 4 | sweets |
| 5 | meats |
表-B
|-id-|-category-|
| 1 | veggies |
| 2 | meats |
| 3 | veggies |
| 4 | veggies |
| 5 | meats |
我需要的是按字母顺序从两个表中选择所有不同的类别。
结果应为:
fruits
meats
sweets
veggies
谢谢
答案 0 :(得分:2)
您应该使用UNION
和ORDER BY
子句:
SELECT DISTINCT category
FROM Table_A
UNION
SELECT DISTINCT category
FROM Table_B
ORDER BY category
答案 1 :(得分:1)
在sql中你可以使用union和order by
select distinct category from (
select category
from table_a
order by category
union
select category
from table_b )