Answer the question
In order to leave comments, you need to log in
MySQL - How to formulate a query correctly?
There is a table of dialogs - dialogs: id | user_one | user_two
And there is a table of users - users: id | name
The dialogue table contains entries of the form 1 | 3 | 2
1 - unique record number
3 - user id
2 - user
id will be 2), that is, if I have ID = 2, I need to get a record from the users table with ID = 3, and if I have ID = 3, then users ID = 2. I hope I explained it clearly.
PS It seems to me that it is not good to fulfill the condition in PHP:
if ($id == 2) {
// select * from users where id = 3
} else {
// select * from users where id = 2
}
Answer the question
In order to leave comments, you need to log in
dachshund, right now I'll try to understand correctly (not very clear) description:
There are tables: dialogs & users
dialogs: id | user_one | user_two
users: id | ...
I understand that you need a request for a la Chat? When a message arrives, what would the name of the interlocutor give you, and yours to him?
almost PHP:
$myID = 2;
// необходимо получить юзера, который не ты, из первой колонки или из второй
$result = mysql_query("SELECT * FROM dialogs WHERE user_one <> '$myID' OR user_two <> '$myID'");
if (!$result) {
die('Сворачиваемся посоны: ' . mysql_error());
}
$myID = 2;
$result = mysql_query("SELECT * FROM users WHERE users.id = ((SELECT user_one FROM dialogs WHERE user_two = '$myID')OR (SELECT user_two FROM dialogs WHERE user_one = '$myID'))");
Not very clear to be honest.
But for any you need to look either towards JOIN, or towards IF
If I understand you correctly:
SELECT u.*
FROM users u
WHERE
(
u.id IN (SELECT user_one FROM dialogs WHERE user_two=ID_ПОЛЬЗОВАТЕЛЯ)
OR
u.id IN (SELECT user_two FROM dialogs WHERE user_one=ID_ПОЛЬЗОВАТЕЛЯ)
)
Not sure if this is what you need, but
select u1.name, u2.name,d.id
from dialogs d
left join users u1 on (d.uid1 = u1.uid)
left join users u2 on (d.uid2 = u2.uid)
You get it from the database, and then in php see who you put in the sender, who in the recipient
, depending on who looks at user1 or user2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question