|
|
 |
| SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Language section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |
|

September 4th, 2004, 07:34 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
UNION and ORDER BY
SELECT <some_columns>
FROM <some_tables>
ORDER BY <some_columns>
LIMIT 25,25
UNION
SELECT <some_columns>
FROM <some_tables>
but MySql says i'm using worng UNION and ORDER BY
What can i do?
|

September 4th, 2004, 02:13 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,477
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ORDER BY cannot be used with the inner select statements that are used for UNION.
You got to use that all the last, because, each select's result is combined together using union operator and ORDER BY can only be applied to the final result set, not for intermediate results. So shift your ORDER BY statement to the last as shown below.
Code:
SELECT <some_columns>
FROM <some_tables>
LIMIT 25,25
UNION
SELECT <some_columns>
FROM <some_tables>
ORDER BY <some_columns>
_________________________
- Vijay G
Strive for Perfection
|

September 5th, 2004, 10:46 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thank you :)
|

September 5th, 2004, 12:23 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
|
|
I can't swear by this as it's DB dependent but if you really need to sort individual sections you can use views that are combined -
VW_Customers:
Code:
SELECT TOP 100% Surname, Forenames FROM tblCustomer ORDER BY Surname
VW_Suppliers:
Code:
SELECT TOP 100% Surname, Forenames FROM tblSupplier ORDER BY Surname
Code:
Select * FROM VW_Customers
UNION ALL
Select * FROM VW_Suppliers
--
Joe
|

September 5th, 2004, 01:03 PM
|
 |
Wrox Author
Points: 33,563, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
Would it be possible to invent an ORDER BY column to order on?
Something like this:
SELECT <some_columns>, '1' AS OrderBY
FROM <some_tables>
UNION
SELECT <some_columns>, '2' AS OrderBY
FROM <some_tables>
ORDER BY OrderBy, <some_columns>
This way, when sorting by, say, surname, the matched records from the first SELECT statement will be listed first, sorted by surname, followed by the ordered records from the second select statement.
Or is there something really wrong with this solution?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

September 5th, 2004, 01:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,477
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
I don't see anything wrong with that. That works perfect. BTW, that was good thinking too.
Cheers!
_________________________
- Vijay G
Strive for Perfection
|

September 17th, 2004, 10:23 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
darcome,
if you are using MySQL (not sure if this would work for other DBs) you can use ORDER BY for each SELECT, you just need to put parentheses around each SELECT statement
http://dev.mysql.com/doc/mysql/en/UNION.html
e.g.
(SELECT <some_columns>
FROM <some_tables>
ORDER BY <some_columns>
LIMIT 25,25)
UNION
(SELECT <some_other_columns>
FROM <some_other_tables>)
ORDER BY <some_other_columns>)
A couple of things to bear in mind are that:
1. Perhaps obviously, each SELECT statement that is being used must contain the same number of columns;
2. The data type of the columns that are used in the first SELECT statement will determine how the data retrieved from subsequent SELECT statements within the UNION are handled.
For instance, if the first column in the first SELECT statement is of type CHAR(5), then even if the first column in the second SELECT statement is of type TEXT, you will only get the first 5 characters of this column regardless of how much data is in it.
A workaround, although I'm sure someone knows a better way is to start the UNION off with a SELECT statement where each column is a type TEXT but doesn't SELECT anything e.g.
(SELECT <some_columns which are TEXT>
FROM <some_tables>
WHERE 1=0)
UNION
(SELECT <some_columns>
FROM <some_tables>
ORDER BY <some_columns>
LIMIT 25,25)
etc etc........
Apparently, this problem has been fixed in version 4.1.1 of MySQL.
cheers
|

September 19th, 2004, 12:15 PM
|
|
Registered User
|
|
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thaks to you too :)
|

June 24th, 2009, 08:22 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, this query works fine but if you only have distinct rows. If you have union from same table but with different filters then you'll have a duplicates in the result set because of the column OrderBY.
Quote:
Originally Posted by Imar
Would it be possible to invent an ORDER BY column to order on?
Something like this:
SELECT <some_columns>, '1' AS OrderBY
FROM <some_tables>
UNION
SELECT <some_columns>, '2' AS OrderBY
FROM <some_tables>
ORDER BY OrderBy, <some_columns>
This way, when sorting by, say, surname, the matched records from the first SELECT statement will be listed first, sorted by surname, followed by the ordered records from the second select statement.
Or is there something really wrong with this solution?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

June 24th, 2009, 04:46 PM
|
 |
Wrox Author
Points: 33,563, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
FYI: the thread is almost 5 years old.... ;-)
Imar
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |