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 06-15-2011, 09:48 PM   PM User | #1
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
UNION Query

I have two tables, table1 and table2.

I want to select everything from table1 and a count of table 2.

Code:
SELECT a. * 
FROM table1 a
UNION SELECT count(b.*) AS bct
FROM table2 b
This is not working. Any thoughts?
mathceleb is offline   Reply With Quote
Old 06-15-2011, 10:35 PM   PM User | #2
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
Read up on how UNION works: You must have the SAME number of fields and type of fields in all branches of the union.

In short, you don't want a union for this.

You might be best off simply making two separate queries. You could combine them into one, but if the data in the two tables are unrelated (as it seems to be), there's not much point.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
mathceleb (06-15-2011)
Old 06-15-2011, 10:44 PM   PM User | #3
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Read up on how UNION works: You must have the SAME number of fields and type of fields in all branches of the union.

In short, you don't want a union for this.

You might be best off simply making two separate queries. You could combine them into one, but if the data in the two tables are unrelated (as it seems to be), there's not much point.
They are completely unrelated. I just wrote two separate queries and that worked fine. Thanks for the tip!
mathceleb is offline   Reply With Quote
Reply

Bookmarks

Tags
union query

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 03:39 PM.


Advertisement
Log in to turn off these ads.