473,785 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UNION ignores my embedded ORDER clause

1 New Member
Hi everybody,
I've got a problem with this SQL-Statement:

Expand|Select|Wrap|Line Numbers
  1. (SELECT * FROM currencies WHERE name = 'EUR') UNION ALL (SELECT * FROM currencies WHERE  name = 'USD')
  2. UNION ALL (SELECT * FROM currencies WHERE name !='EUR' AND name != 'USD' ORDER BY name ASC)
The table "currencies " contains over 100 different currencies and I want get EUR first, then USD and at least the rest of the table in alphabetical order.

But the problem seems to be that the ORDER BY clause in the last SELECT is ignored. The Output is definitly not ordered in any way. I checked this by changing the order-direction to desc.
Am I just to stupid or is this a problem within MySQL ?

Greetz Piecko
Oct 28 '08 #1
1 2052
Atli
5,058 Recognized Expert Expert
That's odd. I would have thought that should work.

I came up with an alternative tho.
You can simply add a integer column to your query and give that column a value that you can then sort the results based on. That is; if the name of the currency is 'EUR', then you give it the value 1. If it is 'USD', then you give it the value 2. If it is anything else, you give it the value 3.
Then you can simply have your ORDER BY clause sort the result by the integer column first, and then by then name column.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     c.`Name`,
  3.     (CASE c.`Name`
  4.         WHEN 'EUR' THEN 1
  5.         WHEN 'USD' THEN 2
  6.         ELSE 3
  7.     END) AS `DisplayOrder`
  8. FROM `Currency` AS c
  9. ORDER BY `DisplayOrder`, `Name` DESC;
  10.  
Oct 30 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
12843
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
3
11976
by: Blake Caraway | last post by:
All, I've seen several posts regarding using UNION or UNION ALL to mash together two or more resultsets into a single result set, but can't seem to find enough info here to help me answer my particular question. I have a stored procedure that gets the column names in a particular format (i.e. "chassis_id"|"chassis_description"|"modify_date") as well as actual data for a given table (in a quote-separated, pipe-delimited
1
1676
by: Tom Schindl | last post by:
Hi, the following Statement worked on MySQL 4.0 but after upgrading to 4.1.12 on win32 the order is not working any more. Is this a known problem or is our SQL simply not useable on 4.1 or is there a bug in MySQL 4.1. ----------------------------8<---------------------------- ( select
3
15256
by: handaman | last post by:
I am having problems retrieving some data from a table, Tablename = "Magazine" Columns MagazineID MagazineName 1 2 Times 3 People 4 National Geographic 5 US
4
3628
by: robert | last post by:
before i go flapping my gums and get deeper in alligators, wondering whether the following makes sense. i have a database which provides picklists for input. it started out with a 2 column key in the cross-reference table to the values table, call them company and state. it's a transitive closure. along the way, naturally, more columns have been added, call them region and division. so that now, the unique identifier from the...
4
2939
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly appreciated. Thank you.
2
4604
by: S. van Beek | last post by:
Dear reader, The following code delivers a wild card in the result of the query. But the ORDER BY is not longer working.
4
9098
by: tkltechy | last post by:
I have a sql query that unions two recordsets. It works when I don't try to adjust the ORDER BY clause using the replace function. However, I want to ensure that a certain item in the list always floats to the top. Can someone help me? Below is the SQL that does not run. Error is "Invalid column name 'name': Select 0 as , 'PRIME' as UNION Select subcontractor.subcontractorID, subcontractor. from subcontractor
3
4004
by: dontfwithchuck | last post by:
Hi, this is my first post. I use Access XP. I have a single table called Movies containing the following fields: ID, Title, Genre, Rating, Location, Date I want a datasheet with the Titles that have a date sorted in Descending order by date, then the Titles that don't have a date sorted by Title in Ascending order. I have this: SELECT & " " & "(" & & ")" AS Expr1
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.