2016-12-14 53 views
-1

我有兩個包含用戶信息和用戶之間的「朋友」關係的表。爲了避免在每個用戶和我選擇定義關係表中沒有關係的用戶之間標記爲'不是朋友'的關係不成爲朋友。如何在逐行基礎上爲無結果子查詢分配默認值?

但是現在我想實現一個搜索功能,幫助用戶添加的人當成自己的朋友,我試圖返回其他用戶的列表誰的名字匹配搜索姓名;我也試圖輸出搜索用戶和結果用戶之間的關係狀態,我試圖使用IFNULL來設置沒有關係時的返回值。

我寫的查詢對單個用戶可以很好地工作,但是當子查詢結果返回多個用戶時,IFNULL語句不會逐行調用,而只會在整個結果表是空的。

這裏是我的表:

user_table 
+----+------------+-----------+ 
| id | first_name | last_name | 
+----+------------+-----------+ 
| 1 | John  | Doe  | 
| 2 | Jane  | One  | 
| 3 | Jane  | Two  | 
| 4 | Jane  | Three  | 
+----+------------+-----------+ 

relationship_table 
+-----------+-------------+--------------+ 
| sender_id | receiver_id | relationship | 
+-----------+-------------+--------------+ 
| 1   | 2   | friends  | 
| 3   | 1   | pending  | 
+-----------+-------------+--------------+ 

我試圖生成時"jane"搜索是由用戶李四執行以下。

results 
+----+------------+-----------+--------------+ 
| id | first_name | last_name | relationship | 
+----+------------+-----------+--------------+ 
| 2 | Jane  | One  | friends  | 
| 3 | Jane  | Two  | pending  | 
| 4 | Jane  | Three  | not friends | 
+----+------------+-----------+--------------+ 

下面是該查詢我目前:

SELECT user_table.id, user_table.first_name, user_table.last_name, derived_table.relationship 
FROM user_table, (
    SELECT IFNULL((type), 'not friends') AS relationship 
    FROM relationship_table 
    WHERE sender_id IN (
     SELECT id 
     FROM user_table 
     WHERE first_name LIKE 'jane%' 
     OR last_name LIKE 'jane%' 
     AND id != '1' 
    ) 
    AND receiver_id = '1' 
    OR receiver_id IN (
     SELECT id 
     FROM user_table 
     WHERE first_name LIKE 'jane%' 
     OR last_name LIKE 'jane%' 
     AND id != '1' 
    ) 
    AND sender_id = '1' 
)derived_table 
WHERE first_name LIKE 'jane%' 
OR last_name LIKE 'jane%' 
AND user_table.id != '1' 

此查詢目前返回此爲"jane"搜索:

+----+------------+-----------+--------------+ 
| id | first_name | last_name | relationship | 
+----+------------+-----------+--------------+ 
| 2 | Jane  | One  | friends  | 
| 2 | Jane  | One  | pending  | 
| 3 | Jane  | Two  | friends  | 
| 3 | Jane  | Two  | pending  | 
| 4 | Jane  | Three  | friends  | 
| 4 | Jane  | Three  | pending  | 
+----+------------+-----------+--------------+ 

很明顯,我這樣做的原因是因爲關係子查詢的全部結果將被添加到每組用戶數據中,而不是匹配相應的用戶。

邏輯告訴我,使用id的連接可以解決這個問題,但是由於哪個用戶發送友誼請求沒有一致性,因此沒有一致的id加入...我不知道如何加入他們正確。

P.S.我敢肯定,可能有一種方法可以更好地檢查發件人或收件人之間的關係,因此對此的任何幫助也將不勝感激。

+1

你需要的,如果你想獲得'NULL'時使用'LEFT JOIN'沒有比賽。 – Barmar

+0

@Barmar如果你想發佈這個解決方案,我會給你正確的答案。謝謝您的幫助。 – Kastoli

+0

它看起來像你自己想出來的。將其作爲答案發布,而不是作爲問題的更新。 – Barmar

回答

0

將所有Janes的表格和John的所有朋友的表格左連接起來。

所有簡氏:

**Query** 
SELECT user_table.id, user_table.first_name, user_table.last_name 
FROM user_table 
WHERE first_name LIKE 'jane%' 
OR last_name LIKE 'jane%' 
AND id != '1' 

**Results** 
+----+------------+-----------+ 
| id | first_name | last_name | 
+----+------------+-----------+ 
| 2 | Jane  | One  | 
| 3 | Jane  | Two  | 
| 4 | Jane  | Three  | 
+----+------------+-----------+ 

約翰的朋友:

**Query** 
(
    SELECT relationship_table.sender_id AS id, relationship 
    FROM relationship_table 
    WHERE receiver_id = '1' 
) 
UNION 
(
    SELECT relationship_table.receiver_id AS id, relationship 
    FROM relationship_table 
    WHERE sender_id = '1' 
) 

**Results** 
+----+--------------+ 
| id | relationship | 
+----+--------------+ 
| 2 | friends  | 
| 3 | pending  | 
+----+--------------+ 

然後組合:

**Query** 
SELECT searched_users.id, searched_users.first_name, searched_users.last_name, IFNULL(users_friends.relationship, 'not friends') AS relationship 
FROM (
    SELECT user_table.id, user_table.first_name, user_table.last_name 
    FROM user_table 
    WHERE first_name LIKE 'jane%' 
    OR last_name LIKE 'jane%' 
    AND id != '1' 
)searched_users 
LEFT JOIN (
    (
     SELECT relationship_table.sender_id AS id, relationship 
     FROM relationship_table 
     WHERE receiver_id = '1' 
    ) 
    UNION 
    (
     SELECT relationship_table.receiver_id AS id, relationship 
     FROM relationship_table 
     WHERE sender_id = '1' 
    ) 
)users_friends 
ON searched_users.id = users_friends.id 

**Results** 
+----+------------+-----------+--------------+ 
| id | first_name | last_name | relationship | 
+----+------------+-----------+--------------+ 
| 2 | Jane  | One  | friends  | 
| 3 | Jane  | Two  | pending  | 
| 4 | Jane  | Three  | not friends | 
+----+------------+-----------+--------------+ 
相關問題