Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql DISTINCT语句如何合并一个表中的两行_Mysql_Sql_Distinct - Fatal编程技术网

Mysql DISTINCT语句如何合并一个表中的两行

Mysql DISTINCT语句如何合并一个表中的两行,mysql,sql,distinct,Mysql,Sql,Distinct,请给出如何从一个表中合并两行的解决方案,然后如何使用distinct 查询: select distinct uid , r_uid from messenger where uid=213 or r_uid =213 我得到了这样的输出 uid r_uid -------------- 213 239 239 213 ----------------- 但我们需要像这样 new id

请给出如何从一个表中合并两行的解决方案,然后如何使用distinct

查询:

select distinct  uid , r_uid from messenger  where uid=213 or r_uid =213 
我得到了这样的输出

      uid     r_uid
     --------------
      213     239
      239     213
     -----------------
但我们需要像这样

           new id
        -----------
            239
         ---------

请给我任何我在MySQL中是新手的解决方案,提前谢谢使用
union

select uid as new_id from messenger
where uid = 213  
union
select r_uid from messenger
where r_uid = 213;   

我认为您正在寻找“其他价值”。最简单的方法可能是:

select uid
from messenger
where r_uid = 213
union -- intentional, remove duplicates
select r_uid
from messenger
where uid = 213;
您也可以使用
案例

select distinct (case when r_uid = 213 then uid else r_uid end)
from messenger
where 213 in (r_uid, uid);

也将返回213。@CforCode…作为OP,您可以接受您喜欢的任何答案。然而,我很好奇,为什么您在一年多后不接受这个答案。