简体   繁体   English

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

[英]How to formulate the query to join more than one table

MY tables are as follows 我的表格如下

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       ...

My Goal: 我的目标:

The product table has 4 types of data. 产品表有4种类型的数据。

  1. Products with no uploader( uploader = 0 ) 没有上传器的产品(上传者= 0)

  2. Products that are in review( exclude = 1 ) 正在审核的产品(不包括= 1)

  3. Products that fall under category = 0(poducts.groupid = product_group.gid AND product_group.category = 0 ) 属于category = 0的产品(poducts.groupid = product_group.gid AND product_group.category = 0)

  4. Products that are uploaded by an uploader, not in review and not fall under category = 0( uploader != 0, exclude = 1, poducts.groupid = product_group.gid AND product_group.category != 0) 由上传者上传的产品,不在审核范围内且不属于category = 0(uploader!= 0,exclude = 1,poducts.groupid = product_group.gid AND product_group.category!= 0)

I only have to consider the 4th type of data. 我只需要考虑第四类数据。 I have to exclude the first three types of data. 我必须排除前三种类型的数据。 I have to group these data by their uploader. 我必须通过他们的上传器对这些数据进行分组。 Say, James have uploaded 3 product, Jones have uploaded 2 product and the rest of the user hasn't uploaded anything. 说,詹姆斯已经上传了3个产品,琼斯已经上传了2个产品,其余的用户还没有上传任何东西。

The query should return this 查询应该返回此信息

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

So if I consider the data of my table, I want to get the data in follwing order 因此,如果我考虑我的表的数据,我想按照以下顺序获取数据

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

I came up with this. 我想出了这个。

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

Which obviously doesn't work as it doesn't include the users, who hasn't uploaded any product. 这显然不起作用,因为它不包括尚未上传任何产品的用户。 How to make this work to take these users into account? 如何使这项工作考虑到这些用户?

EDIT: 编辑:

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

It also doesn't take users with 0 upload. 它也不会使用户上传0。

Second EDIT: 第二次编辑:

I have added a comment table(I forgot about it earlier). 我添加了一个评论表(我之前忘记了)。 Is there any way to show the total_number of comments for a user. 有没有办法显示用户的total_number注释。

Here, James has uploaded product 1, 4, 8. For these post_id is also 1, 4, 8(In real these won't be same). 在这里,詹姆斯已经上传了产品1,4,8。对于这些post_id也是1,4,8(实际上这些将不一样)。 From comments table, these posts have following number of comments 2, 0, 0. So, total number of comment 2. 从评论表中,这些帖子有以下评论数量2,0,0。因此,评论总数2。

So, final result should be 所以,最终的结果应该是

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

Try this query: 试试这个查询:

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

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

To count a number of comments, please add a dependent subquery to the SELECT clause: 要计算多个注释,请在SELECT子句中添加一个从属子查询:

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

in this way: 通过这种方式:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM