Do we have PREFER keyword for SQL?

Discussion in 'MySQL' started by mahmood, Jan 13, 2008.

  1. #1
    Do we have any "PREFER" keyword or something like it? This is the seudo code of what I want:
    SELECT * FROM myTable Preferebly WHERE Title LIKE '%hello%' LIMIT 0,10
    PHP:
    By which I mean: Give me 10 rows of myTable, if you can find hello in Title then include them otherwise whatever the default.

    At the moment I have to use 2 queries, first search for rows where their title like my keyword and then getting the rest of the rows again.
     
    mahmood, Jan 13, 2008 IP
  2. caban

    caban Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe you could try:
    SELECT * FROM myTable WHERE 1 OR Title LIKE '%hello%' LIMIT 0,10
     
    caban, Jan 13, 2008 IP
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Would this return everything? I believe OR would be ignored, I'll give it a try though.:)
     
    mahmood, Jan 13, 2008 IP
  4. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have to say I am not the worlds greatest SQL person (and our SQL guy isnt online) but I cannot think of how to do this as a single query with a limit.

    You could remove the limit and then use a CASE statement to create an extra column with a 0 if the title contains hello and a 1 if it doesnt then just sort by this column.

    Alternatively you can do 2 queries and join the results.
     
    AstarothSolutions, Jan 13, 2008 IP