Hi,
I want to get only the rows with a unique value for a certain field (2 actually). 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. senderID
is the ID of the user who sent the message, receiverID
is the ID of the user who received the message. ...
are some fields (like text
) that don't really matter and they aren't unique either.
So first of all, I want to get all rows where I (#1) am the sender or the receiver.
SQL: "SELECT id FROM table WHERE senderID='1' OR receiverID='1'";
This will return (0, 1, 2, 5)
.
But now, I only want all unique records. So the conditions are:
1
should be the senderID or the receiverID.if ((senderID == '1' && "__ receiverID is not yet listed__") || (receiverID == '1' && "__ senderID is not yet listed__"))
in pseudo-code.
The final return should be (0, 1)
.
How do I do this in (My)SQL? I do know how to do this using PHP, but when there're thousands of records it's not fast enough anymore.
Kind regards,
Tim