Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 09:26 PM   PM User | #1
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 105
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
Ordering union

I have a Union hat I need to order, however I am struggling to get the order correct. I have two tables that are correctly pulling the data. The two order columns use an auto increment to give it an order number. So now I have matching numbers in the columns. This means that ordering goes

A.1
B.1
A.2
B.2

What I really want is the order to be

A.1
A.2
B.1
B.2

I have tried adding names to the columns and ordering by A.orders, B.orders but this wont work. Any ideas for me guys???

Code:
SELECT `order`, field_name FROM all_child_fields WHERE editible='1' UNION SELECT `order`, field_name FROM a_child_fields WHERE editible='1' ORDER BY `order`
itxtme is offline   Reply With Quote
Old 02-03-2013, 11:39 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
something like that:
Code:
select * from 
(
SELECT `order`, field_name FROM all_child_fields WHERE editible='1' 
UNION 
SELECT `order`, field_name FROM a_child_fields WHERE editible='1' 
) as foo
ORDER BY foo.order, foo.field_name
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 02-04-2013, 01:15 AM   PM User | #3
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 105
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
@BubikolRamios

Thanks for the suggestion, I did attempt that prior to my first post but it doesnt work. I have decided to add a 3rd column that is 0 for one table and 1 for the next, I can then order by this column followed by the order column
itxtme is offline   Reply With Quote
Old 02-04-2013, 09:09 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You don't need to add a column to the tables. Just add it to the UNION.

If that's what you meant, then that is the correct way to do it.
Code:
SELECT 0 as tablenumber, `order`, FROM all_child_fields WHERE editible='1' 
UNION 
SELECT 1, `order`, field_name FROM a_child_fields WHERE editible='1' 
ORDER BY tablenumber, `order`
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:03 AM.


Advertisement
Log in to turn off these ads.