How would I order a table by the number of matching params in the where clause of an sql statement?

Posted by Eitan on Stack Overflow See other posts from Stack Overflow or by Eitan
Published on 2010-04-21T08:43:43Z Indexed on 2010/04/21 9:03 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

I'm writing sql to search a database by a number of parameters. How would I go about ordering the result set by the items that match the most parameters in the where clause. For example:

SELECT * FROM users WHERE username = 'eitan' OR email = '[email protected]' OR company = 'eitan'

Username | email | company

1) eitan | [email protected] | blah

2) eitan | [email protected] | eitan

3) eitan | [email protected] | blah

should be ordered like:

2, 3, 1.

Thanks. (ps the query isn't that easy, has a lot of joins and a lot of OR's in the WHERE)

Eitan

© Stack Overflow or respective owner

Related posts about sql

Related posts about order