What is wrong with this SQL Statement?

Discussion in 'PHP' started by adamjblakey, Feb 28, 2008.

  1. #1
    Hi,

    Please can someone help me with this. It is not returning any results but it should.

    SELECT * FROM listings WHERE (category = 'Weddings' or category2 = 'Weddings' or category3 = 'Weddings') AND (subcategory = 'DJs Discos and Karaoke' or subcategory2 = 'DJs Discos and Karaoke' or subcategory3 = 'DJs Discos and Karaoke') AND status = '1' ORDER BY ('premium', 'sdate')
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Feb 28, 2008 IP
  2. quicksolutions

    quicksolutions Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Don't use single quotes in column name after order by clause because it will be considered as string instead of column name.it should be like this
    ORDER BY (premium, sdate)
     
    quicksolutions, Feb 28, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for that, but that still does not return a result when it should. Is there anything else wrong with the statement?
     
    adamjblakey, Feb 28, 2008 IP
  4. quicksolutions

    quicksolutions Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can u give the table structure , i mean it looks that status field is of integer type but you are giving 1 in single quotes
     
    quicksolutions, Feb 28, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Any errors returned from MySQL?
     
    nico_swd, Feb 28, 2008 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    The problem lies with ORDER BY ('premium', 'sdate') when i take this out it works.
     
    adamjblakey, Feb 28, 2008 IP
  7. quicksolutions

    quicksolutions Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok!!!!!!!!!!
     
    quicksolutions, Feb 28, 2008 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    Try ordering by each one individually and see if it works that way.
     
    jestep, Feb 28, 2008 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    I tried doing ORDER BY premium ORDER BY sdate but this does not work.
     
    adamjblakey, Feb 28, 2008 IP
  10. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #10
    Does ORDER BY premium"; work without the sdate at all, and vice-versa?
     
    jestep, Feb 28, 2008 IP
  11. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #11
    Yes that works fine, just not when i use them both??
     
    adamjblakey, Feb 28, 2008 IP
  12. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #12
    Maybe try without parenthesis:

    ORDER BY premium ASC, sdate ASC

    Switch ASC with DESC if you want a descending order.
     
    jestep, Feb 28, 2008 IP
  13. nks

    nks Well-Known Member

    Messages:
    1,602
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    160
    #13
    If status is an integer, you only need to indicate....
     
    nks, Feb 28, 2008 IP
  14. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #14
    That works fine thanks :)
     
    adamjblakey, Feb 28, 2008 IP