Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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