[英]Select distinct from two column
My database like this: table name: messages 我的数据库是这样的:表名:messages
| receive | transmit |
---------------------
| 1 | 5 |
| 1 | 6 |
| 1 | 3 |
| 3 | 1 |
| 4 | 1 |
| 2 | 3 |
| 4 | 6 |
| 7 | 9 |
And i am trying to get all unique id's from each table. 我正在尝试从每个表中获取所有唯一ID。 So my answer must be 1, 3, 4, 5, 6.
所以我的答案必须是1、3、4、5、6。
if i use SELECT DISTINCT receive FROM messages ;
如果我使用
SELECT DISTINCT receive FROM messages ;
i will get 1, 3 ,4 我会得到1,3,4
if i use SELECT DISTINCT transmit FROM messages ;
如果我使用
SELECT DISTINCT transmit FROM messages ;
i will get 1, 3 ,5, 6 我会得到1,3,5,6
how do i get 1, 3, 4, 5, 6? 我如何获得1、3、4、5、6?
The default for UNION
is DISTINCT
: UNION
的默认值为DISTINCT
:
SELECT "from" FROM tableX
UNION
SELECT "to" FROM tableX;
SELECT DISTINCT `fr_om` FROM `table`
UNION
SELECT DISTINCT `to` FROM `table` WHERE `to` NOT IN (SELECT DISTINCT `fr_om` FROM table)
Updated : you can just use UNION to remove the duplicates from your result set. 已更新 :您可以仅使用UNION从结果集中删除重复项。
SELECT DISTINCT `fr_om` FROM `table`
UNION
SELECT DISTINCT `to` FROM `table`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.