gpt4 book ai didi

sql - 两个表中的SQL连接行

转载 作者:行者123 更新时间:2023-12-03 18:34:21 24 4
gpt4 key购买 nike

我不太擅长SQL,想知道是否可以做到这一点:我有两个表:table_a和table_b。两个表都有一个名为category的TEXT类型列。

例:

表_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


谢谢

最佳答案

您应该使用UNIONORDER BY子句:

SELECT DISTINCT category 
FROM Table_A
UNION
SELECT DISTINCT category
FROM Table_B
ORDER BY category

关于sql - 两个表中的SQL连接行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com