Jump to content

ORDER BY question


Canman2005

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965110
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965131
Share on other sites

... 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

Link to comment
https://forums.phpfreaks.com/topic/182849-order-by-question/#findComment-965189
Share on other sites

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.