简体   繁体   English

带有 2 个连接和计数的 Sql 查询和分组依据

[英]Sql query with 2 join and count numbers and group by

I want to use an join to list the car colors count, car type, and users name.我想使用连接来列出汽车颜色计数、汽车类型和用户名。

I have 3 table我有3张桌子

Table 1 Useres表 1 用户

id|username|fullname
1 | test0  | xy xy
2 | test1  | yx yx

Table 2 Car Type表 2 车型

id|car_type|user_id
1 | Ford   | 1
2 | BMW    | 2
3 | Ford   | 1
4 | Skoda  | 1
5 | BMW    | 2

Table 3 Car Color表 3 汽车颜色

id| Color  |user_id|car_id
1 | Red    | 1     |1
2 | Blue   | 2     |2
3 | Red    | 2     |5
4 | Red    | 1     |3
5 | Red    | 1     |4
6 | Green  | 1     |4

One car has 2 color一辆车有2种颜色

The result should be:结果应该是:

countType | CountColor  | UserName
   3      |    4        | test0
   2      |    2        | test1

I tryed this:我试过这个:

   SELECT 
   test as BlogsPost, 
   test2 as CommenstPost,
   u.name   
   FROM users u 
   LEFT JOIN (
       select COUNT(blogs.user_id) as test FROM blogs GROUP by blogs.user_id)  blogs
    on blogs.user_id=u.id 
   LEFT JOIN (
       select COUNT(comments.user_id) as test2 FROM comments GROUP by comments.user_id) comments
    on comments.user_id=u.id 
   GROUP by users.id

I agree with the comment that states the table design is not really well constructed yet for you to achieve the counts you want you will need to do subqueries like this:我同意这样的评论,即表设计还没有真正构建好,您需要执行以下子查询才能实现所需的计数:

SELECT 
(SELECT count(1) from CarType where user_id=username) as countType,
(SELECT count(1) from CarColor where user_id=username) as countColor,
username from (
SELECT username from Users
) a 

As a suggestion for design:作为设计建议:

Table Users表用户

Table Cars桌车

Table Colors表格颜色

Then you have a Relationship table where you have user_id, car_id, color_id然后你有一个关系表,其中有 user_id、car_id、color_id

This would be the proper table design for this structure这将是此结构的正确表设计

If I understand your question correctly with reference to your actual code section what you want is a list of users with how many blogs they have and how many comments they have.如果我参考您的实际代码部分正确理解了您的问题,那么您想要的是一个用户列表,其中包含他们拥有多少博客以及他们有多少评论。 Now if you were wanting to count one matching table you could just do this:现在,如果您想计算一个匹配的表,您可以这样做:

SELECT 
   U.NAME
   ,COUNT(1) AS BLOG_COUNT
FROM USERS U
LEFT JOIN BLOGS B
   ON B.USER_ID = U.ID
GROUP BY U.NAME

But since you are wanting to count two tables you have to do it slightly differently.但是由于您想要计算两个表,因此您必须稍微不同地进行计算。 There's a few ways of doing it but the way I like is like this:有几种方法可以做到,但我喜欢的方式是这样的:

SELECT 
   U.NAME
   ,B.BB_COUNT AS BLOG_COUNT
   ,C.CC_COUNT AS COMMENT_COUNT
FROM USERS U
LEFT JOIN 
(
   SELECT 
      BB.USER_ID
      ,COUNT(1) AS BB_COUNT
   FROM BLOGS BB
   GROUP BY BB.USER_ID
) B
   ON B.USER_ID = U.ID

LEFT JOIN 
(
   SELECT 
      CC.USER_ID
      ,COUNT(1) AS CC_COUNT
   FROM COMMENTS CC
   GROUP BY CC.USER_ID
) C
   ON C.USER_ID = U.ID

That may or may not be the most efficient way but in my experience it works pretty well and it's simple to understand.这可能是也可能不是最有效的方式,但根据我的经验,它运作良好,而且很容易理解。 It all depends a lot on the number of rows in the tables and indexes etc. Usually the idea is to narrow down rows returned as fast as possible.这在很大程度上取决于表和索引等中的行数。通常的想法是尽可能快地缩小返回的行数。 In this case you'll have two sub queries but they'll end up with only as many rows as you have users basically.在这种情况下,您将有两个子查询,但它们最终的行数基本上与您拥有的用户数一样多。

Another thing to note, this will return all users, period.另一件事要注意,这将返回所有用户,期间。 That may not be what you want.那可能不是您想要的。 You might want only a subset of users.您可能只需要一部分用户。 If so this inner select may not be the most efficient because you're doing calculations on users that may not be in the final result, wasting time.如果是这样,这个内部选择可能不是最有效的,因为您正在对可能不在最终结果中的用户进行计算,从而浪费时间。 However I may be getting off topic.不过我可能跑题了。

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

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