MYsql question: how to order by certain letter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • St Lunatic
    Registered User
    • Dec 2002
    • 40

    #1

    MYsql question: how to order by certain letter?

    Im working on a link list and want to show a certian amount of sites in a category and order it by a certain letter.

    If i just use "ORDER by title decs" it will just start with A. How do I say I want it to order by title starting with a certain letter? I want it so if im ordering by C it would list sites that start with C first then list D, E and so on. It seems like it wouldnt be that hard but ive been looking around for awhile trying to figure it out, any help would be appreciated.
    <a href=http://geckofind.com/webmasters/newsignup.php?ref=1><img src=http://geckofind.com/webmasters/banners/b1.jpg></a>
  • dnsmonster
    Confirmed User
    • Jul 2002
    • 634

    #2
    I read that 2x and still not sure what you mean exactly. This will return all records where title value begins with C

    SELECT * FROM table WHERE LEFT(title,1) hahahaha "C"

    replace hahahaha with equals sign.

    Why is equal sign banned? WTF?
    I couldn't possibly know what I'm talking about, I'm completely, absolutely and definitively out of my fucking mind.

    Comment

    • St Lunatic
      Registered User
      • Dec 2002
      • 40

      #3
      Ill give an example of what im trying to do.

      Say i have these sites in my database and the titles are:

      Asian Girls
      Black Girls
      Ebony Girls
      Zoo Girls

      How do I order these sites starting from lets say letter E so they will appear like:

      Ebony Girls
      Zoo Girls
      Asian Girls
      Black Girls

      Hope that helps explain what im trying to do.
      <a href=http://geckofind.com/webmasters/newsignup.php?ref=1><img src=http://geckofind.com/webmasters/banners/b1.jpg></a>

      Comment

      • dnsmonster
        Confirmed User
        • Jul 2002
        • 634

        #4
        I doubt you can do this with a single query in MySQL. It's a two query thing, or you can just do one big select and sort stuff with PHP.
        I couldn't possibly know what I'm talking about, I'm completely, absolutely and definitively out of my fucking mind.

        Comment

        • KevinX
          Registered User
          • Jun 2003
          • 23

          #5
          St Lunatic,

          As dnsmonster stated you would need to do this with 2 queries.

          Assuming you want to sort it by title and the columns name was 'TITLE' you would use the following 2 queries

          SELECT * FROM `table_name` WHERE LEFT(`TITLE`,1) = 'C';

          SELECT * FROM `table_name` WHERE LEFT(`TITLE`,1) != 'C' ORDER BY LEFT(`TITLE`,1) < 'C', LEFT(`TITLE`,1);

          The first query gets all the links with a title that starts with C. The second query gets all links whos titles do not start with c sorting it by whether the left character is greater than C or not with a secondary sort of the leftmost character. This forces it to show up in the format you need while still maintaining the alphabetical listing. If you need any more help feel free to ask.


          Regards,
          Kevin

          Comment

          • St Lunatic
            Registered User
            • Dec 2002
            • 40

            #6
            Thanks guys, running the 2 querys worked did exactly what I was wanting to do.


            Thanks-
            <a href=http://geckofind.com/webmasters/newsignup.php?ref=1><img src=http://geckofind.com/webmasters/banners/b1.jpg></a>

            Comment

            • FovBowBW
              Registered User
              • Apr 2003
              • 4

              #7
              better use

              SELECT * FROM `table_name` WHERE `TITLE`LIKE 'C%';

              as it uses the index (otherwise mysql runs through the whole table). but the TITLE field must be char or varchar in order to index it...

              Comment

              Working...