简体   繁体   English

MySQL仅获取特定字段具有唯一值的行

[英]MySQL get only rows with a unique value for a certain field

I want to get only the rows with a unique value for a certain field (2 actually). 我只想为特定字段(实际上是2)获取具有唯一值的行。 My table is like this: 我的桌子是这样的:

id    senderID    receiverID    ...    ...    ...
_________________________________________________
0     0           1             ...    ...    ...
1     2           1             ...    ...    ...
2     1           0             ...    ...    ...
3     0           2             ...    ...    ...
4     2           0             ...    ...    ...
5     1           2             ...    ...    ...

In this table, id is unique always . 在此表中, id 始终是唯一的。 senderID is the ID of the user who sent the message, receiverID is the ID of the user who received the message. senderID是发送消息的用户的ID, receiverID是接收消息的用户的ID。 ... are some fields (like text ) that don't really matter and they aren't unique either. ...是一些并不重要的字段(例如text ),它们也不是唯一的。

So first of all, I want to get all rows where I (#1) am the sender or the receiver. 因此,首先,我想获得我(#1)是发送者或接收者的所有行。

SQL: "SELECT id FROM table WHERE senderID='1' OR receiverID='1'";

This will return (0, 1, 2, 5) . 这将返回(0, 1, 2, 5)

But now, I only want all unique records. 但是现在,我只想要所有唯一记录。 So the conditions are: 因此条件是:

  1. 1 should be the senderID or the receiverID. 1应该是senderID或receiverID。
  2. if ((senderID == '1' && "__ receiverID is not yet listed__") || (receiverID == '1' && "__ senderID is not yet listed__")) in pseudo-code. if ((senderID == '1' && "__ receiverID is not yet listed__") || (receiverID == '1' && "__ senderID is not yet listed__"))伪代码中的if ((senderID == '1' && "__ receiverID is not yet listed__") || (receiverID == '1' && "__ senderID is not yet listed__"))

The final return should be (0, 1) . 最终的返回值应该是(0, 1)

How do I do this in (My)SQL? 如何在(My)SQL中执行此操作? I do know how to do this using PHP, but when there're thousands of records it's not fast enough anymore. 我确实知道如何使用PHP来执行此操作,但是当有成千上万的记录时,它的速度就不够快了。

Kind regards, 亲切的问候,

Tim 提姆

If you do this, you will get all distinct ID's where you are sender and the distinct id's where you are receiver. 如果执行此操作,您将在发送方和接收方获得所有唯一ID。

Additionally, the UNION will combine both the results. 此外,UNION将合并两个结果。

In your logic, you can use the 2nd column value ('S'/ 'R') to filter separate the 2 sets if you need to do things separately for sent / received id's. 按照您的逻辑,如果您需要对发送/接收的ID分别进行操作,则可以使用第二列的值('S'/'R')来过滤两个集合。

SELECT distinct id, 'S' as Type FROM table WHERE senderID='1' 
UNION
SELECT distinct id, 'R' as Type FROM table WHERE receiverID='1' 
select min(id) from 
(
  select id, senderID pID from table where receiverID = '1'
  union
  select id, receiverID pID from table where senderID = '1'
) as fred
group by pID;

For your data set, this gives: 对于您的数据集,这给出了:

+---------+
| min(id) |
+---------+
|       0 |
|       1 |
+---------+

Something like 就像是

SELECT DISTINCT id, senderID, receiverID FROM table WHERE senderID='1' OR receiverID='1';

?

DISTINCT keyword will remove any duplicates from the result. DISTINCT关键字将从结果中删除所有重复项。 Works good so far, you don't add any other columns other than id, senderID and receiverID. 到目前为止效果很好,除了id,senderID和ReceiverID之外,您无需添加其他任何列。

Otherwise you could also use the GROUP BY clause 否则,您也可以使用GROUP BY子句

SELECT id FROM table WHERE senderID='1' OR receiverID='1' GROUP BY senderID, receiverID;

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

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