简体   繁体   English

涉及外部联接的mysql中多个表上的联接的SQL count(*)问题

[英]SQL count(*) issues with join on multiple tables in mysql involving an outer join

I have three tables, lets say t1, t2, t3 with columns as 我有三个表,假设t1,t2,t3的列为

 t1.c1, t1.c2, t1.c3 
 t2.c4, t2.c1, t2.c5
 t3.c6, t3.c4 

t1.c1, t2.c4 and t3.c6 are primary, autoincrements fields of their respective tables. t1.c1,t2.c4和t3.c6是其各自表的主要自动增量字段。

t2.c1 is a foreign key in t2 referring to t1.c1 in t1 t2.c1是t2中的外键,引用了t1中的t1.c1

t3.c4 is a foreign key in t3 referring to t2.c4 in t2 t3.c4是t3中的外键,引用了t2中的t2.c4

I want to get the following output for any given value of t2.c5 (say X) 对于任何给定的t2.c5值,我想获得以下输出(例如X)

SORRY - Mistake in output columns! 抱歉-输出栏中有误!

I want output to be 我希望输出是

t2.c4, t1.c2, count

and not 并不是

t2.c1, t1.c2, count

where count is the number of records in t3 for the given value of t2.c5 and must be zero if no record exists where t2.c5=X and t3.c4=t2.c4 其中count是给定值t2.c5在t3中的记录数,如果不存在记录,则必须为零,其中t2.c5 = X并且t3.c4 = t2.c4

Can this be done in one select query? 可以在一个选择查询中完成吗?

Here is sample data: 这是示例数据:

Table: t1 表:t1

  +------+-------+------+
  | c1   | c2    | c3   |
  +------+-------+------+
  |    1 | 11111 |  111 |
  |    2 | 22222 |  222 |
  |    3 | 33333 |  333 |
  +------+-------+------+

Table: t2 表:t2

  +------+------+------+
  | c4   | c1   | c5   |
  +------+------+------+
  |    1 |    1 |   11 |
  |    2 |    1 |   11 |
  |    3 |    2 |   11 |
  |    4 |    2 |   12 |
  |    5 |    3 |   12 |
  |    6 |    2 |   12 |
  +------+------+------+

Table: t3 表:t3

  +------+------+
  | c6   | c4   |
  +------+------+
  |    1 |    1 |
  |    2 |    1 |
  |    3 |    5 |
  +------+------+

For t2.c5=11 , the output is 对于t2.c5=11 ,输出为

    +-------------------+
     t2.c4 | t1.c2| Count
    +-------------------+
      1    | 11111| 2
      2    | 11111| 0
      3    | 22222| 0
    +-------------------+

For t2.c5=12 , the output is 对于t2.c5=12 ,输出为

    +-------------------+
     t2.c4 | t1.c2| Count
    +-------------------+
      4    | 22222| 0
      5    | 33333| 1
      6    | 22222| 0
SELECT  t2.c1, t1.c2, count(*)
FROM t1 inner join t2 using (c1)
inner join t3 using (c4)
GROUP BY t2.c5, t2.c1, t1.c2

Whats confusing here is that you want to select t2.c1, t1.c2 for any value of t2.c5. 令人困惑的是,您想为t2.c5的任何值选择t2.c1,t1.c2。

You will have to decide either grouping by all three columns as shown above, or really grouping by t2.c5, and just showing any value of t2.c2, t1.c2 , which will require using some aggregate function on them. 您将必须决定按上述所有三列进行分组,还是根据t2.c5进行分组,仅显示t2.c2的任何值,即t1.c2,这将需要在它们上使用一些聚合函数。 For example: 例如:

SELECT  max(t2.c1) c2, max(t1.c2) c2, count(*)
FROM t1 inner join t2 using (c1)
inner join t3 using (c4)
GROUP BY t2.c5

Try this query: 试试这个查询:

SELECT t2.c4, t1.c2, COUNT(t3.c6) 
FROM t1
INNER JOIN t2 ON t1.c1 = t2.c1
LEFT JOIN t3 ON t3.c4 = t2.c4
WHERE t2.c5 = '11'
GROUP BY t2.c4, t1.c2

Output: 输出:

c4  c2  count
-------------
1   11111   2
2   11111   0
3   22222   0

Demo here 在这里演示

With this query: 使用此查询:

SELECT t2.c4, t1.c2, COUNT(t3.c6) as count
FROM t1
INNER JOIN t2 ON t1.c1 = t2.c1
LEFT JOIN t3 ON t3.c4 = t2.c4
WHERE t2.c5 = '12'
GROUP BY t2.c4, t1.c2

output is: 输出为:

c4  c2      count
-----------------
4   22222   0
5   33333   1
6   22222   0

Demo here 在这里演示

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

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