gpt4 book ai didi

mysql - 从 3 列中选择不同的值到 1

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:01 25 4
gpt4 key购买 nike

我目前正在使用一个 (MySQL) 数据库,该数据库有一个包含 3 列的表,其中包含 INT 值。每列可能有 NULL 和重复值,例如:

enter image description here

我想知道是否可以在 1 个查询中从每一列中选择所有不同的值并将它们合并到 1 个结果列中;在这个例子中是这样的:

col
-----
1
30
40
60

谢谢,

最佳答案

你可以使用联合

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

如果您已经为您的列建立了索引,那么您可以为每 3 个查询使用单独的 where 子句,以便最好地利用您的索引

关于mysql - 从 3 列中选择不同的值到 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403718/

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