php - How to order a MYSQL query by another query?

290

I am writing a PHP/MYSQL search function which takes a users search query (as a string) and matches it to records from a table by predefined fields and quantities (eg: bedrooms = 2, max_price < 100)

This is working fine, but now I want to order the results by a condition. So for example:

  • The users query is "2 Bedroom Penthouse"

  • The function matches records that have a value of 2 for the row bedrooms

I now want to keep the results that match, but order the results by the results that match for a description row with a value of "penthouse" (anywhere in the description string (which is a long string of lots of words))

I can match the results that have the word "penthouse" in the description, but what is the best way to order the results with the matched results first?

So, basically, how do you order results by another query?

355

Answer

Solution:

You don't. You write a more complexORDER BY clause.

SELECT ...
  ORDER BY ..., description NOT LIKE "%penthouse%"

(We invert the expression since false orders before true)

People are also looking for solutions to the problem: Seeking general advice: is there a better way to echo dynamic HTML than heredoc? (smarty / php / html)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.