mapleleaf Posted November 13, 2008 Share Posted November 13, 2008 Is there a MySQL function that can prioritize? For example I want to search for books. The books table contains the city, street,zip and country of the book. If i want the results to show the zip matches, then the city matches, then the state matches and lastly the country matches in that order can it be done with one query? Like zip is 1, city is 2 etc in priority. <?php //so something like: $query = "SELECT * FROM books WHERE title LIKE '%$posted%' AND zip[1] = '$zip' AND city[2] = '$city' AND state[3] = '$state' AND country[4] = '$country' "; ?> This is of course not valid but just to convey the concept. Just had the thought that maybe ORDER by can be used for columns. ORDER BY zip,city,state,country. Help much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/ Share on other sites More sharing options...
fenway Posted November 13, 2008 Share Posted November 13, 2008 Yes, you can do this: $query = "SELECT * FROM books WHERE title LIKE '%$posted%' AND zip = '$zip' AND city = '$city' AND state = '$state' AND country = '$country' ORDER BY ( zip = '$zip' DESC, city = '$city' DESC, state = '$state' DESC, country = '$country' DESC )"; Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/#findComment-689638 Share on other sites More sharing options...
mapleleaf Posted November 13, 2008 Author Share Posted November 13, 2008 Fenway Thanks so much as I'm sure this is close but I am getting this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC, city = 'jamestown' DESC, state = 'CO' DESC, country = 'USA' DESC )' at line 1 Maybe it needs to be more complicated as in having a sub query for each of the ORDER BYs What do you think? Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/#findComment-689650 Share on other sites More sharing options...
fenway Posted November 13, 2008 Share Posted November 13, 2008 Sorry, my bad about the parens.... try: $query = "SELECT * FROM books WHERE title LIKE '%$posted%' AND zip = '$zip' AND city = '$city' AND state = '$state' AND country = '$country' ORDER BY ( zip = '$zip' ) DESC, ( city = '$city' ) DESC, ( state = '$state' ) DESC, ( country = '$country' ) DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/#findComment-689688 Share on other sites More sharing options...
mapleleaf Posted November 13, 2008 Author Share Posted November 13, 2008 Actually it works with all the parenths out But thanks anyway $clause = "WHERE $type LIKE '%$posted%' AND (zip = '$zip' OR city = '$city' OR state = '$state' OR country = '$country') ORDER BY zip = '$zip' DESC, city = '$city' DESC, state = '$state' DESC, country = '$country' DESC"; Is giving me what I wanted Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/#findComment-689711 Share on other sites More sharing options...
corbin Posted November 13, 2008 Share Posted November 13, 2008 Whoa, I never knew that was possible. *Just had his mind blown.* Quote Link to comment https://forums.phpfreaks.com/topic/132619-solved-prioritized-search/#findComment-689816 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.