MySQL从2列中选择不同的值 [英] MySQL Select distinct values from 2 columns

查看:85
本文介绍了MySQL从2列中选择不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个其中有链接的表.因此,除其他外,有2列包含URL.源和目标. 我需要获取表中存在的所有唯一URL的列表(以创建查找表),但是URL当然可以在两列中.在2列上使用distinct似乎会返回唯一的组合,而不是单个值.我可以在1个查询中执行此操作,还是应该使用2个(每列一个),然后对该结果进行3d不同选择?

I have a table wich has links in it. So amongst others, there are 2 columns that contain URLs; Source and Destination. I need to get a list of all the unique urls that are present in the table (to create a lookup table), but a url can be in both columns of course. Using distinct on the 2 columns seems to return unique combinations, rather than single values. Can I do this in 1 query or should I use 2 (one for each column) and then a 3d distinct select on that result?

推荐答案

您可以使用UNION,它将删除重复项(而UNION ALL将保留重复项)

You can use an UNION, which will remove the duplicates (while UNION ALL will keep the duplicates)

SELECT FirstUrlColumn AS url
FROM myTable
UNION
SELECT secondUrlColumn AS url
FROM myTable

这篇关于MySQL从2列中选择不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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