数据库;来自不同类别的列表

标签 mysql sql categories

假设我有下表,名为 test:

enter image description here

我需要选择以获得以下内容:

enter image description here

有人知道如何使用 SQL 来做到这一点吗?

最佳答案

你可以使用UNION ALL:

select category,
    item
from test
where category is not null

union all

select category2 as category,
    item
from test
where category2 is not null
order by item

Demo

关于数据库;来自不同类别的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950108/

相关文章:

php - 使用表中的 LIMIT 选择 'person' 的数据

mysql - SQL:从另一个表中插入具有匹配对象名称的列

sql - SELECT 列表中子查询内的不同 LISTAGG

php - 在 WooCommerce 电子邮件通知中显示订单商品的产品类别

r - 如何对每个样本的多个观察值中的变量进行分箱?

mysql - MySQL数据库中分层存储数据、类别树和SELECT查询

mysql - 查询计算特定日期 +/- 4 周内的数据

sql - Compojure + clojure.contrib.sql : SELECT query is being cached. 为什么?

php - SQL/PHP - 关键更新

c# - 使用 C# 读取 sql 查询