Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Jul 31, 2000

Union Query

Question:
I am writing a SQL statement in Access that is a union query. I am getting an error saying "The number of columns in the two selected tables or queries of a union query do not match." I put NULL's in there to see if it would work, but it doesn't.

I understand I need to have the same amount of fields, but what can I do if I want only select fields in certain SELECT statements?

Answer:
You can use null values for columns that you don't select. However, the column must have the same name. Here's a simple union query where the first table has three columns and the second table has two columns. The third column is just set to NULL in the query:

SELECT name, age, city
FROM TableA
UNION
SELECT name, age, city=null
FROM TableB
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date