Joy
Joy

Reputation: 287

ORDER by number of WHERE that match with MYSQL and Mongodb

Assuming I have following mysql query:

SELECT * FROM user WHERE code='aaa' OR city='bbb' OR state='ddd' LIMIT 1

How can I order by number of matches of WHERE? For example, the first result should have all three conditions that match. If there is no user who has all the three conditions that match, then the first result will have two conditions that match and so on...

How to order in this way with mysql and mongodb?

Upvotes: 2

Views: 71

Answers (1)

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Try this MySQL query

SELECT *,((code='aaa')+(city='bbb')+(state='ddd')) count FROM `user` 
WHERE code='aaa' OR city='bbb' OR state='ddd'order by count DESC

if any condition (code='aaa') is true it will return 1

Upvotes: 1

Related Questions