2017-04-05 102 views
0

什麼應該是合併下面的mySQL查詢的最佳方式?它應該複雜嗎?我想通過match_score對結果進行排序,就像在第一個查詢中一樣。我聽說有一個聯合選項可以加入多個select查詢,但不確定它是如何工作的,但是在一個select查詢下沒有辦法合併3個查詢嗎?謝謝你的時間。如何合併這個mySQL查詢

查詢1:

SELECT 
    ui.User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(my.interest_id) * 2.5 AS match_score 
FROM 
    User_Interests ui 
     LEFT JOIN 
    User_Interests my ON (my.user_id = ? 
     AND my.interest_id = ui.interest_id) 
GROUP BY ui.user_id 
ORDER BY match_score DESC 

問題2:

SELECT 
    ud.User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(my.dislike_id) * 2.5 AS match_score 
FROM 
    User_Dislikes ud 
     LEFT JOIN 
    User_Dislikes my ON (my.user_id = ? 
     AND my.dislike_id = ud.dislike_id) 
GROUP BY ud.user_id 
ORDER BY match_score DESC 

問題3:

SELECT 
    u.* 
FROM 
    User u 
     LEFT JOIN 
    User_Status us ON u.id = User_ID1 AND us.User_ID2 = 5 
WHERE 
    gender = ? 
     AND (us.Status != 'FRIENDS' 
     OR us.status IS NULL) 
     AND u.birthday >= DATE(NOW()) - INTERVAL ? YEAR - INTERVAL 1 YEAR 
     AND u.birthday < DATE(NOW()) - INTERVAL ? YEAR 
+0

第三個查詢中的外連接是什麼?在我看來,根本沒有任何效果。您可以在用戶5上爲用戶找到非朋友記錄,但不管用戶是否選擇該用戶。 (我假設User_ID1 + User_ID2在User_Status中是唯一的,否則加入會複製結果記錄。) –

+0

對於User_Status:條目User_ID1 = 1,User_ID2 = 5與User_ID1 = 5,User_ID2 = 1具有不同的含義。或者保證對於記錄a | b(例如1 | 5)總是有兄弟記錄b | a(例如5 | 1)?或者這兩個陳述都不是真的?如果後者:你是不是想查找'u.id = User_ID2並且us.User_ID1 = 5'呢? –

+0

您應該顯示三個查詢返回的樣本數據,即針對一個結果或兩個或三個結果中出現的四個用戶的某些行以及合併查詢的預期結果。因爲會有兩個分數:你想排序哪個? –

回答

1

你說你想添加分數。好的。您也可以說查詢3用於過濾,但由於外部聯接不會自然過濾任何內容,因此唯一的過濾器就是性別和出生日期。但是,似乎您正嘗試使用用戶5篩選非朋友。我想你正在尋找這樣的東西:

select 
    u.*, 
    i.match_score as interests_match_score, 
    d.match_score as dislikes_match_score, 
    coalesce(i.match_score, 0) + coalesce(d.match_score, 0) as total_match_score 
from user u 
left join 
(
    select 
    ui.user_id, 
    truncate(count(*)/2, 1) + count(my.interest_id) * 2.5 as match_score 
    from user_interests ui 
    left join user_interests my on (my.user_id = ? and my.interest_id = ui.interest_id) 
    group by ui.user_id 
) i on i.user_id = u.id 
left join 
(
    select 
    ud.user_id, 
    truncate(count(*)/2, 1) + count(my.dislike_id) * 2.5 as match_score 
    from user_dislikes ud 
    left join user_dislikes my on (my.user_id = ? and my.dislike_id = ud.dislike_id) 
    group by ud.user_id 
) d on d.user_id = u.id 
where id in (select user_id1 from user_status where user_id2 = 5 and status <> 'friends') 
and gender = ? 
and u.birthday >= date(now()) - interval ? year - interval 1 year 
and u.birthday < date(now()) - interval ? year; 

您可能必須作出修改,但這些應該很容易。

+0

非常感謝!這是我正在尋找的。 –

0

聯盟是聚體的最佳方式ge多個查詢在同一時間。下面是這樣做的方式

SELECT 
    User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(interest_id) * 2.5 AS match_score from { 

    SELECT 
    ui.User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(my.interest_id) * 2.5 AS match_score 
FROM 
    User_Interests ui 
     LEFT JOIN 
    User_Interests my ON (my.user_id = ? 
     AND my.interest_id = ui.interest_id) 

     union 

     SELECT 
    ud.User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(my.dislike_id) * 2.5 AS match_score 
FROM 
    User_Dislikes ud 
     LEFT JOIN 
    User_Dislikes my ON (my.user_id = ? 
     AND my.dislike_id = ud.dislike_id) 

     union 


     SELECT 
    ud.User_ID, 
    TRUNCATE(COUNT(*)/2, 1) + COUNT(my.dislike_id) * 2.5 AS match_score 
FROM 
    User u 
     LEFT JOIN 
    User_Status us ON u.id = User_ID1 AND us.User_ID2 = 5 
WHERE 
    gender = ? 
     AND (us.Status != 'FRIENDS' 
     OR us.status IS NULL) 
     AND u.birthday >= DATE(NOW()) - INTERVAL ? YEAR - INTERVAL 1 YEAR 
     AND u.birthday < DATE(NOW()) - INTERVAL ? YEAR 

} results 

GROUP BY User_ID 
ORDER BY match_score DESC 
+0

現在我有3個錯誤。第5行的1表示意外選擇。第12行的另一個表示缺少跨越大括號,另有一個在第40行中表示缺少跨越大括號。 –

+0

我已經更新了代碼,請檢查並做相應修改 –