MySQL:按字母顺序在多个列中获取唯一值 [英] MySQL: Get unique values across multiple columns in alphabetical order

查看:57
本文介绍了MySQL:按字母顺序在多个列中获取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的桌子是这样的:

If my table looks like this:

id | colA   | colB | colC
===========================
1  | red    | blue | yellow
2  | orange | red  | red
3  | orange | blue | cyan

我要运行什么SELECT查询,以使返回的结果是:

What SELECT query do I run such that the results returned are:

蓝色,青色,橙色,红色,黄色

基本上,我想提取多个列中不同值的集合列表,并按字母顺序返回它们.

Basically, I want to extract a collective list of distinct values across multiple columns and return them in alphabetical order.

我不关心性能优化,因为结果被解析为将用作缓存的XML文件(几乎不更新数据库).因此,即使是肮脏的解决方案也可以.

I am not concerned with performance optimization, because the results are being parsed to an XML file that will serve as a cache (database is hardly updated). So even a dirty solution would be fine.

感谢您的帮助!

推荐答案

(SELECT DISTINCT colA AS color FROM table) UNION
(SELECT DISTINCT colB AS color FROM table) UNION
(SELECT DISTINCT colC AS color FROM table)
ORDER BY color

这篇关于MySQL:按字母顺序在多个列中获取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆