gpt4 book ai didi

MySql。我怎样才能从2个表中得到计数

转载 作者:行者123 更新时间:2023-11-29 19:29:11 25 4
gpt4 key购买 nike

我有 2 个表,TableA 和 TableB

表A

IdA | Date       | Description  
--- | ---------- | -----------
1 | 2017-01-01 | Sometext1
2 | 2017-01-01 | Sometext2
3 | 2017-01-02 | Sometext3
4 | 2017-01-03 | Sometext4
<小时/>

表B

IdB | IdA | Type
--- | --- | ----
1 | 1 | A
2 | 1 | A
3 | 2 | A
4 | 2 | A
5 | 2 | B
6 | 3 | B
7 | 4 | A
<小时/>

我怎样才能获得这样的值(value):

Count(IdA) | Type        
---------- | ----
3 | A

我尝试使用 INNER 或 LEFT JOIN 进行编码

SELECT COUNT(tablea.IdA), tableb.Type
FROM tablea INNER JOIN tableb
ON tablea.IdA=tableb.IdA
WHERE tableb.Type='A'

但它总是让我这样

Count(IdA) | Type
---------- | ----
5 | A

我只想使用 TableB 中的 WHERE 子句对 TableA 行进行计数。可以做这样的事情吗?

最佳答案

您可能需要COUNT(DISTINCT col)

试试这个:

SELECT 
COUNT(DISTINCT a.IdA), b.Type
FROM
tablea a
INNER JOIN
tableb b ON a.IdA = b.IdA
WHERE
b.Type = 'A'
GROUP BY b.type

关于MySql。我怎样才能从2个表中得到计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com