2013-12-12 63 views
0

我想實現一個「添加朋友」頁面,但我不能確定什麼是正確的SQL顯示給用戶所有他可以添加的人。這裏是我的表:SQL請求:顯示不是朋友的人

用戶

-id

-name

友誼

-id_user

-id_friend

這是同一種這篇文章的數據庫:friendship database schema

因爲我想擁有所有誰不是朋友與他的用戶,誰還沒有問他是朋友,誰還沒有被請去當朋友用戶他

編輯: 這是最接近我想要的結果,我曾嘗試:

SELECT * 
FROM user user 
LEFT JOIN friend friend ON user.id=friend.id 
WHERE friend.id IS NULL AND friend.id_friend IS NULL 

,但它只是返回所有誰還沒有得到任何朋友的人。

在此先感謝!

+1

讓我們瞭解您嘗試 – 2013-12-12 01:38:59

+0

完成。我嘗試了很多東西,但沒有得出結論 – user3093547

回答

0

sqlFiddle demo根據您的friendship schema

/* --------------get users who are not friends with 'Tim' */ 
SELECT u2.id,u2.name 
FROM users u1,users u2 
WHERE NOT EXISTS(SELECT 1 
       FROM friendship f 
       WHERE f.id_user = u1.id AND 
         f.id_friend = u2.id) 
    AND NOT EXISTS(SELECT 1 
       FROM friendship f 
       WHERE f.id_user = u2.id AND 
         f.id_friend = u1.id) 
AND u1.id != u2.id 
AND u1.id = 1; 

/* ---------------get users who have not asked 'Tim' to be a friend*/ 
SELECT u2.id,u2.name 
FROM users u1,users u2 
WHERE NOT EXISTS(SELECT 1 
       FROM friendship f 
       WHERE f.id_user = u2.id AND 
         f.id_friend = u1.id) 
AND u1.id != u2.id 
AND u1.id = 1; 

/* ------------- get users who have not been asked by 'Tim' to be a friend */ 
SELECT u2.id,u2.name 
FROM users u1,users u2 
WHERE NOT EXISTS(SELECT 1 
       FROM friendship f 
       WHERE f.id_user = u1.id AND 
         f.id_friend = u2.id) 
AND u1.id != u2.id 
AND u1.id = 1; 

所有三個查詢已經被設置爲自動搜索基於Timu1.id = 1數據,只是改變了最後一個條件到任何用戶ID,你要尋找的。

第一個查詢可以組合成一個OR條件類似下面

/* get users who are not friends with 'Tim' */ 
SELECT u2.id,u2.name 
FROM users u1,users u2 
WHERE NOT EXISTS(SELECT 1 
       FROM friendship f 
       WHERE (f.id_user = u1.id AND 
         f.id_friend = u2.id) 
        OR(f.id_user = u2.id AND 
         f.id_friend = u1.id)) 
AND u1.id != u2.id 
AND u1.id = 1; 

左連接解決方​​案sqlFiddle(你必須更換其中1 ID到您所選擇的用戶ID的比較,他們發生在第一個查詢3次,兩次在第二和第三個查詢)

/* get users who are not friends with 'Tim' */ 
SELECT u.id,u.name 
FROM users u 
LEFT JOIN friendship f ON 
    (u.id=f.id_user AND f.id_friend = 1) 
OR(u.id=f.id_friend AND f.id_user = 1) 
WHERE 
    f.id_friend IS NULL 
AND u.id != 1; 

/* get users who have not asked 'Tim' to be a friend */ 
SELECT u.id,u.name 
FROM users u 
LEFT JOIN friendship f ON 
    (u.id=f.id_user AND f.id_friend = 1) 
WHERE 
    f.id_friend IS NULL 
AND u.id != 1; 

/* get users who have not been asked by 'Tim' to be a friend */ 
SELECT u.id,u.name 
FROM users u 
LEFT JOIN friendship f ON 
    (u.id=f.id_friend AND f.id_user = 1) 
WHERE 
    f.id_friend IS NULL 
AND u.id != 1; 
+0

非常感謝你這是我一直在尋找! – user3093547

0

沒有朋友跟他

這應做到:

SELECT * 
FROM users U 
WHERE NOT id IN (
    SELECT F.id_friend 
    FROM friendship F 
    WHERE U.id = F.id_friend 
    AND F.id_user = 1 
) 
AND id != 1 

或者

SELECT U.*, F.* 
FROM users U 
LEFT JOIN friendship F 
    ON U.id = F.id_friend 
    AND F.id_user = 1 
WHERE F.id_user IS NULL 
    AND U.id != 1 

然而,

誰沒有要求他是朋友,誰沒有被要求與他的朋友

你沒有這個信息在你的模式中可用。

編輯:太早:在其他鏈接的問題解釋/

+0

感謝您的回答,但它只返回沒有任何朋友的人。我應該在哪裏放置關於給定用戶的條件,以向他展示他不是朋友的人? – user3093547