我有以下贷款架构:
mysql> select * from borrower;
+----------+---------+
| name | loan_id |
+----------+---------+
| Adams | L16 |
| Curry | L93 |
| Hayes | L15 |
| Jackson | L14 |
| Jones | L17 |
| Smith | L11 |
| Smith | L23 |
| Williams | L17 |
+----------+---------+
8 rows in set (0.00 sec)
我需要计算每个人的贷款总数。我认为自联接是解决方案,我尝试了以下查询:
select T1.name, count(T2.name) from borrower T1, borrower T2 where T1.name = T2.name;
但是我得到了如下奇怪的结果:
mysql> select T1.name, count(T2.name) from borrower T1, borrower T2 where T1.name = T2.name;
+-------+----------------+
| name | count(T2.name) |
+-------+----------------+
| Adams | 10 |
+-------+----------------+
1 row in set (0.00 sec)
答案 0 :(得分:2)
你可以试试这个:
select name, count(name) from borrower
group by name