Jump to content

Union and Order BY


lalnfl

Recommended Posts

So say I have a union sql statement like this:

 

(SELECT id, card_id, ordered FROM Match_1 WHERE card_id='$card_id') UNION (SELECT id, card_id, ordered FROM Match_2 WHERE card_id='$card_id') UNION (SELECT id, card_id, ordered FROM Match_3 WHERE card_id='$card_id') ORDER BY ordered ASC

 

ordered is an INT, but yet when I order the results its like this

 

1

11

2

 

11 should obviously be last but it isn't, what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/237677-union-and-order-by/
Share on other sites

So I go back to my original response.  The implication is that there is something you are doing wrong in the code that displays the results.

 

Could it be because I am using parenthesis around each SELECT part (SELECT id, card_id, ordered FROM Match_1 WHERE card_id='$card_id') UNION (...

Link to comment
https://forums.phpfreaks.com/topic/237677-union-and-order-by/#findComment-1221423
Share on other sites

You could have saved a lot of time if you had actually checked what we suggested you check, rather than taking the short cut and looking at one table of the 3.

 

What do you mean? Everyone was set at INT, except for one of them, that I accidentally skipped.

 

Exactly right.  requinix pointed out that the data types were a concern.  We have mo way of knowing what the data types were, but we assumed that you checked all of them, not one of three.  In the future don't cut corners.  That is the lesson that hopefully you will learn.  Consistency is an important aspect of successful development practices.  It doesn't matter to requinix or myself.  Neither of us would have made the mistake you made.

Link to comment
https://forums.phpfreaks.com/topic/237677-union-and-order-by/#findComment-1221477
Share on other sites

You could have saved a lot of time if you had actually checked what we suggested you check, rather than taking the short cut and looking at one table of the 3.

 

What do you mean? Everyone was set at INT, except for one of them, that I accidentally skipped.

 

Exactly right.  requinix pointed out that the data types were a concern.  We have know way of knowing what the data types were, but we assumed that you checked all of them, not one of three.  In the future don't cut corners.  That is the lesson that hopefully you will learn.  Consistency is an important aspect of successful development practices.  It doesn't matter to requinix or myself.  Neither of us would have made the mistake you made.

 

Well excuse me, didn't know you had to be perfect. lol

Link to comment
https://forums.phpfreaks.com/topic/237677-union-and-order-by/#findComment-1221484
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.