复杂的SQL联盟

时间:2014-10-07 17:03:08

标签: mysql sql union

任何SQL Guru都在那里我可以使用一些帮助!我正在创建一个我认为需要Union的存储过程,以便所有结果都以1 SELECT语句返回。 我已将问题简化为下表:

用户

user_id username name DOB ------------------------------------------------------

1 JohnSmith1 John Smith 01/01/1990

2 LisaGreen17 Lisa Green 03/07/1986

3 BarneyB Barney Brown 09/12/1960

user_team

user_team_id user_id team_id total_score -------------------------------------------------------------

1 1 1 29

2 2 7 37

3 3 2 15

private_league

priv_league_id league_name host_user league_password -------------------------------------------------------------------------

1 Lisa's League 2 CSUASH429d9

2 Barney's Bonanza 3 Jkap89f5I01

user_team_private_league_M2M

id priv_league_id user_team_id ----------------------------------------

1 1 1

2 1 2

3 1 3

4 2 1

5 2 3

我想运行一个带有 user_id 输入的存储过程,它会带回用户输入的所有联赛每个人的主机那些联赛每个联赛中有多少玩家参赛每个联赛中用户所处的位置(按总分排序)。< / p>

目前我有:

CREATE DEFINER=`root`@`localhost` PROCEDURE `user_private_leagues`(IN v_user_id INT)
BEGIN

    DECLARE userteamid INT;

    # Retrieve user team from a user_id
    SELECT user_team_id INTO userteamid
    FROM user_team 
    WHERE user_id = v_user_id;

    # Retrieve private league name and host user (for a userteam)
    SELECT private_league.league_name, private_league.host_user
    FROM   user_team_private_league_M2M
    INNER JOIN privateleague
    ON user_team_private_league_M2M.priv_league_id=private_league.priv_league_id
    WHERE  user_team_id = userteamid;

END

此查询包含每个联盟的玩家总数以及当前用户的位置

我创建了一个查询来恢复每个私人联盟的总用户数,没有像这样的用户过滤器:

SELECT private_league_id, COUNT(*) AS total_users
FROM   classicseasonmodel_classicseasonuserteamprivateleague
GROUP BY private_league_id;

可以使用this question的答案并使用total_score来计算用户当前位置的查询。

此刻我非常坚持这一点 - 来自SP的完美结果如下:

CALL user_private_leagues(3);(BarneyB的用户ID)

priv_league_name current_position total_users host_user -----------------------------------------------------------------------

Lisa's League 3 3 LisaGreen17

Barney's Bonanza 2 2 BarneyB

谢谢!

2 个答案:

答案 0 :(得分:0)

让我们一步一步......

第一个身份证明,请求理解

SELECT private_league_id, COUNT(*) AS total_users
FROM   classicseasonmodel_classicseasonuserteamprivateleague
GROUP BY private_league_id;

现在添加名称和主机用户

SELECT PL.league_name, LC.uCNT, PL.host_user  
FROM (SELECT private_league_id AS pID, COUNT(*) AS uCNT
      FROM   classicseasonmodel_classicseasonuserteamprivateleague
      GROUP BY private_league_id ) AS LC
LEFT JOIN private_league PL ON PL.priv_league_id = LC.pID

现在添加主机用户名

SELECT PL.league_name, LC.uCNT as total_users, hu.name as host_user
FROM (SELECT private_league_id AS pID, COUNT(*) AS uCNT
      FROM   classicseasonmodel_classicseasonuserteamprivateleague
      GROUP BY private_league_id ) AS LC
LEFT JOIN private_league PL ON PL.priv_league_id = LC.pID
LEFT JOIN user hu ON PL.host_user = hu.user_id

不知道目前的位置在哪里。


此查询将为每个团队提供用户和职位,加入此协议并按用户ID限制,以便为每个团队获得一个用户职位:

select UT.user_id, 
       UT.team_id, 
       ROW_NUMBER() OVER (PARTITION BY team_id ORDER BY total_score DESC) AS team_position
from private_league L
join user_team_private_league_M2M LJ ON L.priv_league_id = LJ.priv_league_id 
join user_team UT ON LJ.user_team_id = UT.user_team_id

答案 1 :(得分:0)

很抱歉,但我没有创建数据库来测试下面的SQL。但你可以从那里开始。无需UNION。我不了解计算用户在联盟中的位置的业务规则,因为它可能来自团队或用户。

select priv_league_id, league_name, host_user_name, count(*) as total_users
from (
      select A.priv_league_id, A.league_name, D.name as host_user_name, B.user_team_id, C.user_id, D.
      from private_league A
      join user_team_private_league_M2M B
      on A.priv_league_id = B. priv_league_id
      join user_team C
      on B.user_team_id = C. user_team_id
      join user D
      on A.host_user = D.user_id
) D
group by priv_league_id