如何合并此mySQL查询

如何合并此mySQL查询,mysql,Mysql,合并以下mySQL查询的最佳方法是什么?它应该变得复杂吗?我想按照match_分数对结果进行排序,就像在第一个查询中一样。我听说有一个union选项可以连接多个select查询,但不确定它是如何工作的,但是没有办法将3个查询合并到一个select查询下吗?谢谢你抽出时间 问题1: 问题2: 问题3: 联合是同时合并多个查询的最佳方法 SELECT User_ID, TRUNCATE(COUNT(*) / 2, 1) + COUNT(interest_id) * 2.5 AS m

合并以下mySQL查询的最佳方法是什么?它应该变得复杂吗?我想按照match_分数对结果进行排序,就像在第一个查询中一样。我听说有一个union选项可以连接多个select查询,但不确定它是如何工作的,但是没有办法将3个查询合并到一个select查询下吗?谢谢你抽出时间

问题1:

问题2:

问题3:


联合是同时合并多个查询的最佳方法

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

你说你想增加分数。可以您还说查询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;

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

现在我有3个错误。1位于第5行,表示意外选择。第12行还有1个表示缺少交叉花括号,第40行还有一个表示缺少交叉花括号。我已经更新了代码。请检查并根据第三个查询中的外部联接是什么进行更改?在我看来,这根本没有效果。您可以在用户5上查找某个用户的非好友记录,也可以不查找,但您可以选择该用户。我假设User_ID1+User_ID2在User_状态中是唯一的。否则,联接将复制结果记录。至于用户\状态:条目用户\ ID1=1,用户\ ID2=5的含义与用户\ ID1=5,用户\ ID2=1的含义不同?或者可以保证记录a | b例如1 | 5总是有兄弟记录b | a例如5 | 1?或者这两种说法都不正确?如果是后者:是否也要查找u.id=User_ID2和us.User_ID1=5?您应该显示三个查询返回的示例数据,即一个或两个或三个结果中存在的四个用户的一些行,以及合并查询的预期结果。因为将有两个分数:您想按哪个排序?我想将前两个查询中的两个分数相加。它们存在的唯一原因是我可以把它们数一数。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
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
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;