[英]Making a sub-query on query results?
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 我在这里尝试了几件事: http : //www.sqlfiddle.com/#!2/ 85d6d/ 36
First query selects two way friends from the table. 第一个查询从表中选择两个方式的朋友。 I get friend_id from that query.
我从该查询中获得friend_id。 Now I want to know more about this friend_id.
现在,我想了解更多关于这个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.
因此,我想使用此ID进行另一个查询,该查询将显示该用户来自连接表的所有word_id,word_ids可用于从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. 有了您的原始查询,我所做的就是将该查询视为一个表(a),并使用
connections
表将其LEFT JOIN
。 Then, LEFT JOIN
the connections
table with the words_en
table to reach the desired words. 然后,将
LEFT JOIN
connections
表与words_en
表连接到所需的单词。 This makes it possible to get all users that return from your original query, even when there are no connections/words. 这样,即使没有连接/单词,也可以获得从原始查询返回的所有用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.