justin7410 Posted June 17, 2013 Share Posted June 17, 2013 I have a pretty beginner's question, but cant seem to get this right with what i try. I had the same problem with using LIKE and OR with multiple fields and queries. essentially i just want to ORDER BY using multiple fields i have in the DB. my query is something like this $query = "SELECT * FROM content WHERE rating > 7 ORDER BY `votes` DESC, `release_date` DESC "; now i understand with LIKE and OR you must separate arguments with (), but with ORDER BY i am not using any AND / OR so i though a comma should be sufficient. Yet, i can never seem to get this work properly. any suggestions to get both fields to ORDER BY ? thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/ Share on other sites More sharing options...
requinix Posted June 17, 2013 Share Posted June 17, 2013 That is the correct syntax. What's not working? now i understand with LIKE and OR you must separate arguments with ()If you're talking about what I think you're talking about, that's only when you need to change how the default behavior, and it mostly just happens when mixing ANDs and ORs. If you say something like "I want all posts by requinix or justin7410 and that have been made since June 1st" then you might write WHERE username = "requinix" OR username = "justin7410" AND date >= "2013-06-01"but what that actually says is "posts by requinix, or posts by justin7410 this month". Because ANDing happens before ORing*. If you add parentheses WHERE (username = "requinix" OR username = "justin7410") AND date >= "2013-06-01"then you override that and get the desired result. * In terms of precedence and what happens when you mix the two together. They are still evaluated left to right. Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436502 Share on other sites More sharing options...
justin7410 Posted June 18, 2013 Author Share Posted June 18, 2013 That is the correct syntax. What's not working? If you're talking about what I think you're talking about, that's only when you need to change how the default behavior, and it mostly just happens when mixing ANDs and ORs. If you say something like "I want all posts by requinix or justin7410 and that have been made since June 1st" then you might write WHERE username = "requinix" OR username = "justin7410" AND date >= "2013-06-01"but what that actually says is "posts by requinix, or posts by justin7410 this month". Because ANDing happens before ORing*. If you add parentheses WHERE (username = "requinix" OR username = "justin7410") AND date >= "2013-06-01"then you override that and get the desired result. * In terms of precedence and what happens when you mix the two together. They are still evaluated left to right. I apologize but how is this pertaining to ORDER BY argument from what i see your only addressing something that i already understand with AND / OR conditional , i am simply confused on ORDER BY sequence. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436510 Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 (edited) but how is this pertaining to ORDER BY argumentRead the first sentence I wrote. You know, what came in my post before I quoted that thing you said and (I would argue) the comment one could deduce was what the remainder of my reply addressed. from what i see your only addressing something that i already understand with AND / OR conditionalI was clarifying something you did not, in fact, understand correctly. Edited June 18, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436514 Share on other sites More sharing options...
justin7410 Posted June 18, 2013 Author Share Posted June 18, 2013 sorry i misread completely sorry for the post above. the syntax is correct and i thought as much, what i am trying to do essentially is quite simple i thought. i am trying to sort my results given from my $query( see OP ) to bring back the results by ORDER OF, of 2 separate fields in the DB. i want it to return ORDER BY `votes`, `release_date`. which i am pretty certain my original query should be returning, but only sorts by the first field in the conditional. as for your original reply, thank you, that was more helpful than i had originally thought. When i said i was having issues with AND/OR statements, i was referring to an issue i had posted, but i was already quite clear on what is needed for SQL statements, more so just confused as why im not getting the results i was hoping for. thanks again req i look forward to more feedback Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436519 Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 (edited) i want it to return ORDER BY `votes`, `release_date`. which i am pretty certain my original query should be returning, but only sorts by the first field in the conditional.It'll sort by the first field first, and when two values are the same then it'll sort by the second field. Seems like you need something more complicated than that. Have some sample data and how you would want it sorted? [edit] Example: a ASC b ASC a ASC b DESC a DESC b DESC a | b a | b a | b a | b --+-- --+-- --+-- --+-- 1 | 1 1 | 1 1 | 3 3 | 3 2 | 2 1 | 2 1 | 2 2 | 3 3 | 3 1 | 3 1 | 1 2 | 2 1 | 2 2 | 2 2 | 3 1 | 3 1 | 3 2 | 3 2 | 2 1 | 2 2 | 3 3 | 3 3 | 3 1 | 1 Edited June 18, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436523 Share on other sites More sharing options...
Brendon77 Posted June 18, 2013 Share Posted June 18, 2013 Query of ORDER BY with multiple columns: Select column_1, column_2, column_3 from table_name order by "creteria"; Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1436577 Share on other sites More sharing options...
Brendon77 Posted August 6, 2013 Share Posted August 6, 2013 (edited) Query of ORDER BY with multiple columns: Select column_1, column_2, column_3 from table_name order by "creteria"; Creteria means, you which select the column. Edited August 6, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/279285-using-order-by-with-multiple-columns/#findComment-1443631 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.