开发者

Getting all unique values between two columns in mysql

I have a mysql table that has two fields that store the same type of information. I want to retrieve all unique values in those two fields.

If it were just one field I could do:

SELE开发者_StackOverflowCT distinct FIELD1 FROM table

How can I get all unique values from FIELD1 and FIELD2

Clarification: I don't mean unique pairs.

Say field1 contains 1,13,5,25,13,8 and field2 contains 6,10,1,30,13

I want a query that returns 1,13,5,25,8,6,10,30


Try this

select distinct * FROM (
  select distinct field1 as n from table
    union 
  select distinct field2 as n from table) as t;


select  field1 ,field2 FROM tablename group by field1 ,field2


select field1 from table
union 
select field2 from table


You mean all unique pairs? Then this might do (a bit dirty, though):

SELECT distinct CONCAT(FIELD1, '|', FIELD2) FROM table


You can do a union of two sql statements:

Select distinct FIELD1 From table
UNION
Select distinct FIELD2 From table
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜