如何制定查询以加入多个表

时间:2014-03-02 18:01:06

标签: php mysql join

我的表格如下

users
id     name
 1     Michael
 2     James
 3     John
 4     Susie
 5     Harvey

products
pid    name          uploader    post_id     views    exclude      groupid
 1     learn_java        2           1         21       0             1
 2     learn_sql         1           2         8        0             2
 3     4 GB DDR3         0           3         5        0             3
 4     love jacket       2           4         0        0             5
 5     1 TB HDD          3           5         12       1             4
 6     kill_ants         3           6         5        0             6
 7     2 TB HDD          2           7         2        0             4
 8     8 GB DDR3         2           8         18       0             3
 9     1 GB DDR2         3           9         7        0             3

product_group
gid    name    category
1      text    1
2      pdf     1
3      ram     2
4      hdd     2
5      leather 0
6      diy     0

product_category
cid    name
1      book
2      electronics

/* forgot about comment field*/
comments
comment_id    post_id    comment
     1            1       ...
     2            1       ...
     3            2       ...
     4            2       ...
     5            2       ...
     6            3       ...

我的目标:

产品表有4种类型的数据。

  1. 没有上传者的产品(上传者= 0)

  2. 正在审核的产品(不包括= 1)

  3. 属于类别= 0的产品(poducts.groupid = product_group.gid AND product_group.category = 0)

  4. 由上传者上传的产品,不在审核范围内且不属于类别= 0(上传者!= 0,排除= 1,poducts.groupid = product_group.gid AND product_group.category!= 0)< / p>

  5. 我只需要考虑第四类数据。我必须排除前三种类型的数据。我必须通过他们的上传器对这些数据进行分组。比如说,詹姆斯已经上传了3个产品,琼斯已经上传了2个产品,其余用户没有上传任何东西。

    查询应返回此

    3 James SUM对3个产品的看法 2 Jones SUM对2个产品的看法 0 user1 0 0 user2 0 .... ....

    因此,如果我考虑我的表的数据,我想按照以下顺序获取数据

    product_num    users.id    users.name    total_views
         3            2           James           41(21+18+2)
         1            3           Jones           7
         1            1           Michael         8
         0            5           Harvey          0/NULL
         0            4           Susie           0/NULL
    

    我想出了这个。

    SELECT COUNT(pid) as product_num,
           SUM(views) as total_views
           users.*
    FROM users
    INNER JOIN products ON products.uploader = users.id
    INNER JOIN product_group ON products.groupid = product_group.category
    WHERE exclude = 0
    AND product_group.category != 0
    

    这显然不起作用,因为它不包括尚未上传任何产品的用户。如何让这项工作考虑到这些用户?

    修改

    SELECT COUNT(pid) as product_num,
           SUM(views) as total_views
           users.*
    FROM users
    LEFT JOIN products ON products.uploader = users.id
    INNER JOIN product_group ON products.groupid = product_group.category
    WHERE exclude = 0
    AND product_group.category != 0
    GROUP BY users.id
    ORDER BY product_num
    

    它也不会让用户上传0。

    第二次编辑:

    我添加了评论表(我之前忘记了)。有没有办法显示用户的total_number评论。

    在这里,詹姆斯上传了产品1,4,8。对于这些post_id也是1,4,8(实际上这些不一样)。从评论表中,这些帖子的评论数量为2,0,0。因此,评论总数为2。

    所以,最后的结果应该是

    product_num    users.id    users.name    total_views      total_comments
         3            2           James           41(21+18+2)       2
         1            3           Jones           7                 0/NULL
         1            1           Michael         8                 3
         0            5           Harvey          0/NULL            0/NULL
         0            4           Susie           0/NULL            0/NULL
    

1 个答案:

答案 0 :(得分:2)

尝试此查询:

SELECT COUNT(pid) as product_num,
       SUM(views) as total_views,
       u.*
FROM users u
LEFT JOIN products p
  ON p.uploader = u.id
LEFT JOIN product_group pg
  ON p.groupid = pg.gid
WHERE p.exclude = 0 
  AND p.uploader <> 0 
  AND pg.category != 0
   OR p.pid is null
GROUP BY u.id

演示:http://sqlfiddle.com/#!2/c94bef/12

要计算多个注释,请在SELECT子句中添加一个从属子查询:

         SELECT count(*)
         FROM comments c
         WHERE c.post_id = p.post_id

以这种方式:

SELECT COUNT(pid) as product_num,
       SUM(views) as total_views,
       ( SELECT count(*)
         FROM comments c
         WHERE c.post_id = p.post_id
       ) As total_comments,
       u.*
FROM users u
LEFT JOIN products p
  ON p.uploader = u.id
LEFT JOIN product_group pg
  ON p.groupid = pg.gid
WHERE p.exclude = 0 
  AND p.uploader <> 0 
  AND pg.category != 0
   OR p.pid is null
GROUP BY u.id

演示:http://sqlfiddle.com/#!2/5c44f/1