---
title: "做个笔记，根据列A统计列B的数量"
author: "wikimo"
date: "2010-02-03T15:07:17Z"
tags: []
categories: ["数据库"]
slug: "a-b"
draft: false
lastmod: "2010-02-03T15:07:17Z"
id: 586752
url: "https://wikimo.iteye.com/blog/586752"
---

需求描述：

根据列username，对列subject的数量进行统计

 

表的记录集：

+----+---------+----------+

| id | subject | username |

+----+---------+----------+

|  1 | 记录一  | admin    |

|  2 | 记录二  | admin    |

|  3 | 记录三  | admin    |

|  4 | 记录四  | test1    |

|  5 | 记录五  | test2    |

|  6 | 记录六  | test2    |

+----+---------+----------+

 

需要的结果：

+----------+-----+

| username | num |

+----------+-----+

| admin    |   3 |

| test1    |   1 |

| test2    |   2 |

+----------+-----+

 

 

SQL：select username,count(subject) as num from 表名 group by username