Canman2005 Posted November 24, 2009 Share Posted November 24, 2009 Hi all I want to do the following on my query ORDER BY m.order ASC which works fine, but s it possible to do the above ORDER BY but always have row `id` number 1 at the first? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/182849-order-by-question/ Share on other sites More sharing options...
Psycho Posted November 24, 2009 Share Posted November 24, 2009 Assuming that the IDs are numeric - i.e. 1 would come before the others, then you could create a dynamic value that is a 1 (for the first record) or 2 otherwise. Then sort by that value first, then by the 'order' field. Something like this: SELECT *, IF(m.id=1, 1, 2) as initOrder FROM tablename m ORDER BY m.initOrder ASC, m.order ASC Quote Link to comment https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965110 Share on other sites More sharing options...
Canman2005 Posted November 25, 2009 Author Share Posted November 25, 2009 Hi Yep, it is a numeric number. I have altered my query to SELECT m.id, m.parentId, m.name, c.url, IF(m.id=1, 1, 2) as initOrder FROM menu m JOIN content c ON m.contentId = c.id WHERE ((m.parentId = 0) || (m.parentId = 1)) m.initOrder ASC, m.order ASC but that seems to return a error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965131 Share on other sites More sharing options...
Psycho Posted November 25, 2009 Share Posted November 25, 2009 ... that seems to return a error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource any ideas? Yes, two. 1. Add an "or die (mysql_error())" to the line where you run your queery so you can see the mysql errors (but I would suggest some 'proper' error handling once it goes to production to prevent users from seeing system errors). 2. What the heck happened to the "ORDER BY"? You have the conditions but not he the actual text for the ORDER BY Try this: SELECT m.id, m.parentId, m.name, c.url, IF(m.id=1, 1, 2) as initOrder FROM menu m JOIN content c ON m.contentId = c.id WHERE m.parentId = 0 OR m.parentId = 1 ORDER BY initOrder ASC, m.order ASC Quote Link to comment https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965189 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.