简体   繁体   中英

How to create MYSQL query to get specific results (count all unique values from one column)

Please look at my table's structure:

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

I would like to create a query (in PHP), to get such results (in 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>
SELECT count(name),
       name 
  FROM MyTable 
  GROUP BY name 
  ORDER BY name ASC;

I assumed your table is named MyTable exchange it accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM