How do I unite these two?
This query:
SELECT f1.asked_user_id AS friend_id
FROM friends AS f1 JOIN friends AS f2
ON f1.asked_user_id = f2.asker_user_id
AND f1.asker_user_id = f2.asked_user_id
AND f1.asker_user_id = :user_id
WHERE f1.status = 1 AND f2.status = 1
With:
(SELECT GROUP_CONCAT(words_en.word) FROM connections JOIN words_en ON connections.word_id = words_en.id WHERE connections.user_id = friends.friend_id) As friend_words
And do yet another join:
JOIN users ON friends.friend_id = users.id
I've tried few things here: http://www.sqlfiddle.com/#!2/85d6d/36
First query selects two way friends from the table. I get friend_id from that query. Now I want to know more about this friend_id. So I want to use this id to do another query that would display all word_ids from connections table by that user, word_ids can be used to get actual words from words_en. And lastly, I want to get users name_surname from users table.
Try this:
SELECT a.name_surname,GROUP_CONCAT(Distinct w.word Order by w.word asc) AS words
FROM (
SELECT f1.asked_user_id AS friend_id,
f1.created,
u.name_surname,
u.avatar
FROM friends AS f1
INNER JOIN friends AS f2 ON f1.asked_user_id = f2.asker_user_id
INNER JOIN users AS u ON f1.asked_user_id = u.id
AND f1.asker_user_id = f2.asked_user_id
AND f1.asker_user_id = 1
WHERE f1.status = 1 AND f2.status = 1
) a
LEFT JOIN connections c ON c.user_id = a.friend_id
LEFT JOIN words_en w ON c.word_id = w.id
GROUP BY 1;
Having your original query, what i did is treat that query as a table (a) and LEFT JOIN
it with connections
table. Then, LEFT JOIN
the connections
table with the words_en
table to reach the desired words. This makes it possible to get all users that return from your original query, even when there are no connections/words.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.