Jump to content

[SOLVED] mysql search query


aldrin151

Recommended Posts

Hello...I'm having problems retrieving records in certain order...for example I have

 

SELECT * FROM `users` WHERE (`1_tut`=1 or `2_tut`=1 or `3_tut`= 1) AND (`1_av`=1 or `2_av`=1 or `3_av`=1 or `4_av`=1) ORDER BY (`1_tut` and `2_tut` and `3_tut`) and (`1_av` and `2_av` and `3_av` and `4_av`) DESC

 

or without bracketing the order by clause

 

SELECT * FROM `users` WHERE (`1_tut`=1 or `2_tut`=1 or `3_tut`= 1) AND (`1_av`=1 or `2_av`=1 or `3_av`=1 or `4_av`=1) ORDER BY `1_tut` and `2_tut` and `3_tut` and `1_av` and `2_av` and `3_av` and `4_av` DESC

 

All of the fields either have a 0 or 1 value stored and I'm trying to sort the records starting with the ones which have the most matching 1 value ... however I'm getting the record which has the least 1 value first...

 

Can someone help me or point out what I'm doing wrong ...

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/117097-solved-mysql-search-query/
Share on other sites

You may try this:

 

SELECT * FROM `users` WHERE (`1_tut`=1 or `2_tut`=1 or `3_tut`= 1) AND (`1_av`=1 or `2_av`=1 or `3_av`=1 or `4_av`=1) ORDER BY `1_tut` DESC,`2_tut` DESC, `3_tut` DESC, `1_av` DESC, `2_av` DESC, `3_av` DESC, `4_av` DESC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.