正确使用连接

时间:2009-08-21 16:59:49

标签: sql mysql join subquery

我正在寻找关于如何优化这个的提示....

SELECT   u.uid,
         u.username,
         u.nid,
         wd.pay
FROM     (users_data AS u
          LEFT JOIN winners_data wd
            ON u.nid = wd.nid
               AND u.uid = wd.uid)
         LEFT JOIN dp_node dn
           ON u.nid = dn.nid
WHERE    u.uid = ".$val."
         AND ((dn.TYPE = 'event'
               AND (SELECT Count(nid)
                    FROM   tournament_event
                    WHERE  nid = u.nid
                           AND type_value IN ('A','B','C')) > 0)
               OR (dn.TYPE = 'new_event'
                   AND (SELECT Count(nid)
                        FROM   user_tournament_event
                        WHERE  nid = u.nid
                               AND type_0_value IN ('Y','X')) > 0))
ORDER BY nid ASC

实际上$ val只是来自以下查询的uid,一次一个作为我的循环的一部分。

SELECT DISTINCT(dump.uid)FROM leader_Jdump AS dump

有没有办法将此部分添加到上述查询中?我想如果我能在MySQL级别这样做会更快。

3 个答案:

答案 0 :(得分:4)

试试这个:

SELECT  u.uid, u.username, u.nid, wd.pay
FROM    users_data AS u
LEFT JOIN
        winners_data wd
ON      (wd.nid, wd.uid) = (u.nid, u.uid)
JOIN    dp_node dn 
ON      dn.nid = u.nid
        AND dn.type IN ('event', 'new_event')
WHERE   u.uid = ".$val."
        AND EXISTS
        (
        SELECT  NULL
        FROM    tournament_event te
        WHERE   te.nid = u.nid
                AND type_value IN ('A', 'B', 'C')
                AND dn.type = 'event'
        UNION ALL
        SELECT  NULL
        FROM    user_tournament_event te
        WHERE   te.nid = u.nid
                AND type_0_value IN ('X', 'Y')
                AND dn.type = 'new_event'

        )
ORDER BY
        u.nid ASC

我从OUTER JOIN删除了dp_node,因为您的原始查询要求NULL子句中dp_node字段的非WHERE条件,因此{{ 1}}在这里没用。

创建以下索引:

LEFT JOIN

答案 1 :(得分:0)

这可能相同,但更容易阅读:

WITH NIDs(type_value,nid) AS (
  SELECT 'event', nid FROM tournament_event
  WHERE type_value IN ('A','B','C')
  UNION ALL
  SELECT 'new_event', nid FROM user_tournament_event
  WHERE type_value IN ('X','Y')
)

SELECT
  u.uid,
  u.username,
  u.nid,
  wd.pay
FROM users_data AS u JOIN NIDs AS n
ON n.nid = u.nid
AND n.type_value = u."type"
LEFT JOIN winners_data AS wd
ON u.nid=wd.nid AND u.uid=wd.uid
LEFT JOIN dp_node AS dn 
ON u.nid = dn.nid
WHERE u.uid=".$val."
ORDER BY nid ASC

如果“什么样的事件”是一个重要的事件,那么你最好只用一张表,你可以通过查看一两列来做出决定。您似乎有两个表格来保存此信息(tournament_event和user_tournament_event)。例如,您可以将信息存储在此查询的NID表达式中。

于2009-08-22添加:

这是相同的查询,CTE表示为派生表:

SELECT
  u.uid,
  u.username,
  u.nid,
  wd.pay
FROM users_data AS u JOIN (
  SELECT 'event', nid FROM tournament_event
  WHERE type_value IN ('A','B','C')
  UNION ALL
  SELECT 'new_event', nid FROM user_tournament_event
  WHERE type_value IN ('X','Y')
) AS n(type_value,nid)
ON n.nid = u.nid
AND n.type_value = u."type"
LEFT JOIN winners_data AS wd
ON u.nid=wd.nid AND u.uid=wd.uid
LEFT JOIN dp_node AS dn 
ON u.nid = dn.nid
WHERE u.uid=".$val."
ORDER BY nid ASC

答案 2 :(得分:0)

我已经看过这种风格(单行末尾的所有右括号),并想知道人们如何理解它。有人早些时候要求重新格式化版本:

SELECT    u.uid,
          u.username,
          u.nid,
          wd.pay
FROM      users_data u
          LEFT JOIN winners_data wd
               ON u.nid = wd.nid
               AND u.uid = wd.uid
          LEFT JOIN dp_node dn
               ON u.nid = dn.nid
WHERE     u.uid = ".$val."
          AND (
                (
                    dn.TYPE = 'event'
                    AND (
                        SELECT Count(nid)
                        FROM   tournament_event
                        WHERE  nid = u.nid
                               AND type_value IN ('A','B','C')
                    ) > 0
                )
                OR (
                    dn.TYPE = 'new_event'
                    AND (
                        SELECT Count(nid)
                        FROM   user_tournament_event
                        WHERE  nid = u.nid
                               AND type_0_value IN ('Y','X')
                    ) > 0
                )
          )
ORDER BY  nid ASC