SQL:如何从第一个表的第二个表获取总数?

时间:2018-08-01 04:51:51

标签: mysql sql

我正在尝试创建一个执行总计数,查找不同值并显示外键的sql查询。

示例:

    : Table 1 :
ID |   ADDRESS.  |LD
____________________________
1  |123 ABC road | 1  
2  |123 ABC road | 1  
3  |123 ABC road | 1  
4  |456 DEF road | 1  
5  |456 DEF road | 2  
6  |456 DEF road | 2  
7  |789 GHI road | 2  
8  |789 GHI road | 3  
9  |987 JKL road | 4  
10 |987 JKL road | 4  

     : OUTPUT : 

Count|   ADDRESS.  |LD
____________________________
3    |123 ABC road | 1  
3    |456 DEF road | 2  
2    |789 GHI road | 3  
2    |987 JKL road | 4  

4行

2 个答案:

答案 0 :(得分:0)

尝试一下:

select count(address), address,max(LD) as LD from Table 1
group by address

答案 1 :(得分:0)

尝试以下

SELECT COUNT(*), ADDRESS, LD FROM Table_1
GROUP BY ADDRESS, LD
HAVING COUNT(*) > 1

让我知道这是否对您有用!