Solved

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Posted on 2009-04-11
2
1,922 Views
Last Modified: 2012-05-06
Why the following sql has the error:

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.


select a.id, shortcode, c.currency, a.type from account a inner join currency c on a.currencyid = c.id WHERE a.id > -1 union select -2 as id, '-- Please Select --' as shortcode from account a order by a.id

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 50 total points
ID: 24122731
Try this query out:

Removed ORDER BY clause because UNION doesn't allows it.

SELECT a.id,shortcode,c.currency,a.type
FROM   account a
INNER JOIN currency c
  ON   a.currencyid = c.id
WHERE  a.id > -1 
UNION 
SELECT -2 AS id,'-- Please Select --' AS shortcode, NULL, null
FROM   account a

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24122810
you can use order by (no points here) like this:

SELECT a.id,shortcode,c.currency,a.type
FROM   account a
INNER JOIN currency c
  ON   a.currencyid = c.id
WHERE  a.id > -1 
UNION 
SELECT -2 AS id,'-- Please Select --' AS shortcode, NULL, null
FROM   account a
ORDER BY 1

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question