简体   繁体   中英

SQL distinct between different columns

I have a table that contains 2 ids that I want to retrieve in a query, and I want them to be distinct. However I want them to be distinct between columns, so:

| column1 | column2 |
|    2    |    3    | row1
|    2    |    4    | row2
|    3    |    2    | row3
|    3    |    2    | row3

should return row1 and row2. Any help?

EDIT:

Sorry, that was sorta vague and I was in a rush when posting. So both of these columns are IDs that reference the same table. Let's think of this as a message between users, where the table in question is a message and the IDs are the user IDs (one for sender, one for receiver). So the messages table looks a little like this:

| sender_id | receiver_id |
|    2      |    3        | message1
|    2      |    3        | message2
|    3      |    2        | message3

Each user can message any other as much as they want, so I can have many messages between each user. I want to know which users a given user has sent a message to. So I need a query to find who the user with ID 2 has messaged. So this theoretical query, given user ID 2, should only return user ID 3. I don't actually care about these messages that I'm searching through, just the users associated with them, who are not the user I'm searching against, and I want a list with unique values.

I've tried something like:

SELECT sender_id, receiver_id FROM messages
WHERE sender_id = 2 OR receiver_id = 2
GROUP BY sender_id
GROUP BY receiver_id

and

SELECT DISTINCT sender_id, receiver_id FROM messages
WHERE sender_id = 2 OR receiver_id = 2

but these only return a distinct list for one of the IDs. (ie. it would return user ID 3 twice ). I hope this is enough info to help.

**

UPDATE:

**

this is the query I ended up using:

SELECT DISTINCT other_user_id FROM (
  SELECT sender_id AS some_user_id,
         receiver_id AS other_user_id FROM messages
  UNION
  SELECT receiver_id AS some_user_id,
         sender_id AS other_user_id FROM messages
)
WHERE some_user_id = whatever_value

Assuming that column1 and column2 are both numeric data types and you are comparing 2 columns you can use the following

SELECT DISTINCT
    LEAST( column1 , column2 ) AS leastColumn,
    GREATEST( column1 , column2 ) AS greatestColumn
FROM yourTable

I'm not entirely sure why you would wish to do this though, as if the two columns store the same data then this table is part of an n:m relationship and so this could be further normalised. Additionally this would be come horribly inefficient as the dataset grows but for your specific question this fits the bill.

Figured it out. This is the query I ended up using:

SELECT DISTINCT other_user_id FROM (
  SELECT sender_id AS some_user_id,
         receiver_id AS other_user_id FROM messages
  UNION
  SELECT receiver_id AS some_user_id,
         sender_id AS other_user_id FROM messages
)
WHERE some_user_id = whatever_value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM