E
E
Evgeny Konkin2015-10-15 22:24:36
PHP
Evgeny Konkin, 2015-10-15 22:24:36

Complex MySQL query?

Hi all!
I apologize in advance for such an impudent, I would say question)))
On the site, users add each other as friends!
There is a table where all this is fixed, but I can’t figure out how to correctly compose a mysql query to the database.
The friends table
cb91f25fb3264189bf50fc486d595bf5.jpg
The bottom line is that you need to display three blocks where it will be shown:
1. FRIENDS (these are those who added each other as friends)
2. SIGNED (these are those whom I added as friends)
3. SUBSCRIBERS (these are those who added me to friends)
I can't make the first request! I think ... It is necessary to select all the users whom I added as friends, and then check if they added me as friends, here I just don’t understand how to make a request!
Please tell me who is well versed in this or good)) how to make a request?
Thanks in advance!
Maybe someone will throw a link where it says something like that)
Thanks too!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2015-10-15
@konnn

From the point of view of performance with large amounts of data, the most correct thing for the first request is to keep a separate plate like yours, but add data there only if the friendship is mutual.
Update it accordingly when adding/removing friendships.

R
Rsa97, 2015-10-17
@Rsa97

SELECT `t1`.`friendid`
    FROM `friends` AS `t1`
    JOIN `friends` AS `t2` ON `t2`.`userid` = `t1`.`friendid`
        AND `t2`.`friendid` = `t1`.`userid`
    WHERE `t1`.`userid` = :myId

And the `id` field in this table is unnecessary, the pair (`userid`, `friendid`) should itself form the primary key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question