简体   繁体   English

不同列之间的SQL不同

[英]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. 我有一个表,其中包含要在查询中检索的2个ID,并且希望它们与众不同。 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. 应该返回row1和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. 因此,这两列都是引用同一表的ID。 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). 让我们将其视为用户之间的消息,其中所讨论的表是消息,而ID是用户ID(一个用于发送者,一个用于接收者)。 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. 因此,我需要查询以查找ID为2的用户已发消息的用户。 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. 因此,在给定用户ID 2的情况下,此理论查询应仅返回用户ID3。我实际上并不关心正在搜索的这些消息,仅关心与之关联的用户,而不是我要搜索的用户,我想要一个具有唯一值的列表。

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. 但是这些只会返回其中一个ID的唯一列表。 (ie. it would return user ID 3 twice ). (即,它将两次返回用户ID 3)。 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 假设column1和column2都是数字数据类型,并且您要比较2列,则可以使用以下内容

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. 我不太确定为什么要这样做,就像两列存储相同的数据一样,那么此表是n:m关系的一部分,因此可以进一步规范化。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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