SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Order by issue, convoluted

    Hello,

    Below is a complete section of my query. I am trying to order by t.age_group but the values don't lend themselves to being ordered the way I want it to be ordered.

    The values can begin with O, R or U and I need to order them like this:

    R, O, U,

    Should I make a new table column for an ordering value, which can be assigned when the record is created or, can this query be amended to do what I need, with the existing data?

    Code MySQL:
    LEFT OUTER
      JOIN ( SELECT live_product_id 
                  , GROUP_CONCAT(
                           CONCAT_WS(','
                               , t.age_group
                               , t.tariff
                               , t.tariff_terms_abbr
                               , t.currency
                               , tt.tariff_terms
                                 ) ORDER
                                      BY t.age_group
                      SEPARATOR ';'
                                   ) AS tariffs
               FROM tariffs as t
               inner join tariff_terms as tt
               on t.tariff_terms_abbr = tt.tariff_terms_abbr
             GROUP
                BY live_product_id
                                        ) AS tb

    Bazz

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use something like
    Code:
    order by case c
    when 'R' then 1
    when 'O' then 2
    when 'U' then 3
    else 4
    end desc

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I seem to recall a post from the past, by longneck, which basically said you can't manipulate returned data in an order by clause.

    The R, O and U is the first letter of each value to be returned.

    So I think I need to try a case in my sub select.

    I'll post back if/when I get stuck.

    bazz

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haven't been able even to think through what the query should be like so I changed the value of my col so that the current 'order by' works.

    I know - chicken
    bazz

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you can't manipulate returned data in an ORDER BY clause?

    that couldn't've been longneck, because of course you certainly can

    Code:
    ORDER BY FIELD(LEFT(t.age_group,1),'R','O','U'), t.age_group
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apologies to longneck.

    I had moved on in my research and was looking into regexes, where a similar question received that answer. not relevant to my OP.

    @rudy,

    Thank you for that code. I'll go try it out now.

    bazz

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As anticipated - that's tickety-boo, rudy.

    bazz

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    tickety-boo is what i do ™

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know


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
  •