简体   繁体   English

从3列中选择不同的值作为1

[英]Select distinct values from 3 columns into 1

I'm currently working with a (MySQL) database wich has a table with 3 columns containing INT values. 我目前正在使用(MySQL)数据库,该数据库的表包含3个包含INT值的列。 Each column may have NULL and repeated values, as such: 每列可能具有NULL和重复的值,例如:

在此处输入图片说明

I'd like to know if it's posssible to, in 1 query, select all distinct values from each column and merge them into 1 resulting column; 我想知道是否有可能在1个查询中,从每一列中选择所有不同的值,然后将它们合并为1个结果列; in this example something like: 在此示例中,类似:

col
-----
 1
 30
 40
 60

Thanks, 谢谢,

You can use union 你可以用工会

select * from (
select col1 as col from table
union 
select col2 as col from table
union
select col3 as col from table
) t where col is not null
order by col

If you have indexed your columns then you can use separate where clauses for each 3 queries in order to best utilize your index 如果您已为列建立索引,则可以为每个3个查询使用单独的where子句,以便最佳利用索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM