Home > Net >  mysql Any duplicate of two fields
mysql Any duplicate of two fields

Time:07-29

raw data

filtered data

I want userid = 1 or object = 1. Imagine that there are only three pieces of data that are not repeated, tried many methods, but they will always be repeated

I want to take the data with userid or object 1, but don't repeat it.

If userid=1 and object = 2, don't the data with object=1 and userId=2. If object=1 and userId=2, don't use the data with userid=1 and object = 1

select userId, object from userChat where (userId = 1 or object = 1) group by userId, object;

CodePudding user response:

you can using SELECT TOP 1 userId, object form userChat where (userId = 1 or object = 1) or SELECT userId, object form userChat where (userId = 1 or object = 1) LIMIT 1

CodePudding user response:


Can you please explain more.. what is the repeated values? Perhaps your base data of information is incorrect. Before using the group by, try to query the data and retrieve the information you need, then, if needed use the group by .

Following your explanation, Just write it as you say it...

   select userId, object 
    from userChat 
    where (userId = 1 and object <> 1) or (object=1 and userId<>1)

CodePudding user response:

I have found a way. First, find out the set of two groups of data and intersect them, that is, there is no duplicate data result. It feels amazing

select id from
(
    select object as id from userChat where (userId = 1)
    union all
    select userId as id from userChat where (object = 1)
) t
group by id order by id;
  • Related