2

要获得选定用户的待处理请求,我使用:

SELECT f1.asker_user_id AS friend_id
FROM friends AS f1 
LEFT JOIN friends AS f2
    ON f1.asked_user_id = f2.asker_user_id
    AND f1.asker_user_id = f2.asked_user_id   
WHERE f1.status = 1 AND f2.status IS NULL
AND f1.asked_user_id = 2

http://www.sqlfiddle.com/#!2/0f7a0d5/65它正确返回用户 3. VALUES (3,2,1)

但我想获得更多关于这个用户 3 的数据。

我需要name_surnameavatarusers表。并且该用户的所有word_ids 来自connectionstable,作为回报,它将显示wordfrom words_enbased on word_id

感谢分配!

ps我试过这个:

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 
  LEFT 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
  WHERE f1.status = 1 AND f2.status IS NULL
       AND f1.asker_user_id = 2
) 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;
4

2 回答 2

1

这是以下查询的SQL Fiddle,这是我认为您所追求的。

SELECT users.name_surname, users.avatar,
GROUP_CONCAT(DISTINCT words_en.word Order by words_en.word asc) AS words
FROM 
(
SELECT f1.asker_user_id AS friend_id
    FROM friends AS f1 
    LEFT JOIN friends AS f2
        ON f1.asked_user_id = f2.asker_user_id
        AND f1.asker_user_id = f2.asked_user_id   
    WHERE f1.status = 1 AND f2.status IS NULL
    AND f1.asked_user_id = 2
) AS MainQ
INNER JOIN users ON MainQ.friend_id = users.id
INNER JOIN connections ON users.id = connections.user_id
INNER JOIN words_en ON words_en.id = connections.word_id
于 2013-11-05T14:38:44.110 回答
1

基于这个问题和你以前的问题,我想你可能正在寻找这样的东西:

SELECT u.name_surname, 
       u.avatar,
       GROUP_CONCAT(DISTINCT w.word Order by w.word asc) AS words
FROM users u
INNER JOIN 
(
SELECT f1.asker_user_id AS friend_id
    FROM friends AS f1 
    LEFT JOIN friends AS f2
        ON f1.asked_user_id = f2.asker_user_id
        AND f1.asker_user_id = f2.asked_user_id   
    WHERE f1.status = 1 AND f2.status IS NULL
    AND f1.asked_user_id = 2
) a ON a.friend_id = u.id
LEFT JOIN connections c ON u.id = c.user_id
LEFT JOIN words_en w ON w.id = c.word_id
GROUP BY u.id;

sqlfiddle demo

于 2013-11-05T14:55:09.020 回答