如何创建MYSQL查询以获取特定结果(计算一列中的所有唯一值)

时间:2011-12-11 12:46:28

标签: php mysql html unique

请查看我的表结构:

-------------
| id | name |
-------------
| 1  |  a   |
| 2  |  a   |
| 3  |  a   |
| 4  |  b   |
| 5  |  b   |
| 6  |  c   |
| 7  |  d   |
| 8  |  d   |

我想创建一个查询(在PHP中),以获得这样的结果(用HTML格式):

<p>name: a (number of letters: 3)</p>
<p>name: b (number of letters: 2)</p>
<p>name: c (number of letters: 1)</p>
<p>name: d (number of letters: 2)</p>

1 个答案:

答案 0 :(得分:5)

SELECT count(name),
       name 
  FROM MyTable 
  GROUP BY name 
  ORDER BY name ASC;

我假设您的表名为MyTable,因此会进行更换。