Results 1 to 3 of 3

Thread: Need help with MySQL select

  1. #1
    Join Date
    Jun 2007
    Beans
    54

    Question [SOLVED] Need help with MySQL select

    Hi!

    I have a database that contains names and surnames in it. I need to write a SELECT that would return me a count of surnames for each letter of the alphabet. In other words, i need to get something like that:

    A | B | C | D | E |..
    0 | 5 | 3 | 4 | 1 | ...

    I can get results for each separate letter by executing
    Code:
    SELECT COUNT(SURNAME) as A FROM USERS WHERE SURNAME LIKE '%A'
    , but that would mean executing ~30 selects. Is there a way to combine all these selects at once?
    Last edited by AndyBoy_LV; August 4th, 2010 at 12:58 AM. Reason: Thread solved

  2. #2
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Need help with MySQL select

    Something like

    Code:
    SELECT SUBSTR(surname, 1, 1) AS firstch, count(*)  FROM users GROUP BY firstch
    should work.

  3. #3
    Join Date
    Jun 2007
    Beans
    54

    Re: Need help with MySQL select

    Quote Originally Posted by Some Penguin View Post
    Something like

    Code:
    SELECT SUBSTR(surname, 1, 1) AS firstch, count(*)  FROM users GROUP BY firstch
    should work.

    Thank you very much, that did the job

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •