English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I would like to have a mysql query that counts how many items there are for each thing that starts with each letter...
For Example: My database would have these items: "Bob", "Susie", "Billy", and "That One Kid". The result would be "B:2 S:1 T:1"

2007-03-14 06:39:10 · 2 answers · asked by Jared W. 3 in Computers & Internet Programming & Design

2 answers

Create two queries. The first one will take this table and return a one-column table with the first letter extracted:

qry1:
firstLetter
B
S
B
T

The exact string manipulation functions in MySQL escape my memory right now.

Then make another query that uses qry1 as input. Group By the firstLetter field for the first column, then add a second column as an expression COUNT(firstLetter).

qry2:
firstLetter, CountOffirstLetter
B, 2
S, 1
T, 1

2007-03-14 06:54:37 · answer #1 · answered by Anonymous · 0 0

I would go for a CURSOR;
Loop inside a a list from a to z; execute simple count query for some thing like 'a%' .. some thing like 'b%' ..etc. in each iteration append the result of count to the alphabet character: and finally adding to a declared type to hold the results

2007-03-14 06:57:30 · answer #2 · answered by Coosa 2 · 0 0

fedest.com, questions and answers