mysql - 如何从行和列中一起选择不同的值

标签 mysql sql

This is my database table.

这是我的数据库表,其中包含一些值。从 topic1 到 topic8 有不同的主题名称。 我想从所有这些值中访问不同的主题名称。 它的 sql 查询是什么?

最佳答案

使用联合查询:

select topic1 as topic from t union
select topic2 as topic from t union
select topic3 as topic from t union
select topic4 as topic from t union
select topic5 as topic from t union
select topic6 as topic from t union
select topic7 as topic from t union
select topic8 as topic from t;

如果您不需要 NULL,请包含 WHERE 子句:

select topic1 as topic from t where topic1 is not null union
select topic2 as topic from t where topic2 is not null union
select topic3 as topic from t where topic3 is not null union
select topic4 as topic from t where topic4 is not null union
select topic5 as topic from t where topic5 is not null union
select topic6 as topic from t where topic6 is not null union
select topic7 as topic from t where topic7 is not null union
select topic8 as topic from t where topic8 is not null;

关于mysql - 如何从行和列中一起选择不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38187670/

相关文章:

php - MySQL - 查询数据库的日期范围

mysql - 我应该在此查询中使用什么来替换 GROUP BY?

sql - 按顺序更新列值,其中新值基于 'previous' 行中的更新值

php - 使用两个外键将两个表连接在一起

mysql - 预订系统

mysql - 在包含 10 亿行的表中查找重复条目

mysql - 在Mysql中从html标签中查找并替换子字符串中的url

mysqldump mysql 8,在命令行中没有密码的Windows服务器上

SQL 查询 : Facing issues to get desired records from different tables

mysql - 在 MySQL 中对 ID 进行分组时获取整行