I want to see all users which following me or another user.
I have got used 3 tables
users
- id
- username
user_follow
- id
- user_id
- follow_id
- date
images
- id
- user_id
- image
I tried to get username with last image uploaded and also I need my and followers status to see if I already follow them or no.
My query is written on Mysql :
SELECT
u.id AS user_id,
u.username,
i.image,
t.id AS STATUS
FROM
users AS u
INNER JOIN (user_follow AS f)
ON (u.id = f.user_id)
LEFT OUTER JOIN images i
ON i.id =
(SELECT
b.id
FROM
images AS b
WHERE f.user_id = b.user_id
ORDER BY b.id DESC
LIMIT 1)
LEFT OUTER JOIN user_follow t
ON t.id =
(SELECT
m.id
FROM
user_follow m
WHERE user_id = 3
AND follow_id = u.id)
WHERE f.follow_id = 7
AND f.user_id NOT IN (7, 3)
GROUP BY f.user_id
My user id - 3; I watch users who are following user with id - 7
And output is :
Array
(
[0] => Array
(
[user_id] => 6
[username] => 6666
[image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
[status] => 226
)
)
I think that my query is too heigh and I do not know how to simple check status and output 1 or 0 . Now I get a follow line id if "follow" and empty if no. Also I will be happy if you show me how to optimaize my query.