在子查询上添加INNER JOIN

时间:2013-03-27 20:21:16

标签: php mysql sql

给定两个表,nf_users和nf_wr_year以及以下查询,如何在nf_users表中添加内部联接以选择其他列?

SELECT 
    points, 
    played, 
    wins 
FROM (
    SELECT
        p.*,
        @i:=@i + 1 rank,
        @match:=IF(p.points = 528, @i, @match)
    FROM
        nf_wr_year p,
        (SELECT @i:=0, @match:=0) vars
    ORDER BY p.points
) t
WHERE @match >= rank - 2 
    AND @match <= rank + 2

1 个答案:

答案 0 :(得分:0)

在引用nf_wr_year之后立即添加联接:

SELECT u.[additional-columns-you-want]
...
FROM
    nf_wr_year p
    join nf_users u
        on u.[something] = p.[something],
    (SELECT @i:=0, @match:=0) vars
ORDER BY p.points
...

确保 t 选择中没有任何重复的列名称,例如您通常通过SELECT p.*, u.* ...

加入的人