如何合并此mySQL查询

时间:2017-04-05 11:59:20

标签: mysql

合并以下mySQL查询的最佳方法是什么?应该变得复杂吗?我想通过match_score对结果进行排序,就像在第一个查询中一样。我听说有一个联合选项可以加入多个选择查询,不确定它是如何工作的但是没有办法在一个选择查询下合并3个查询?谢谢你的时间。

查询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

2 个答案:

答案 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;

您可能需要进行修改,但这些应该很容易。

答案 1 :(得分:0)

Union是同时合并多个查询的最佳方式.Below是这样做的方式

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