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
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
Share on other sites

After posting a re-looking at the code and then redoing it in the phpmyadmin sql query thing, it doesn't display correctly. And the reason is if I declare more than 2 UNION statements, it messes up the results.

 

Do you know why it does that?

Link to comment
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.

Link to comment
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
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
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.