Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: UNION Query

  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    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?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    mathceleb (06-15-2011)

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •