Results 1 to 5 of 5
  1. #1
    jasonbarnes is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2010
    Location
    NC
    Posts
    56

    Union & union all

    What's the correct syntax for these queries with multiple tables? I can't seem to find an example anywhere online.

    Somewhere I read to replicate as follows...

    TABLE TBLONE UNION ALL SELECT * FROM TBLTWO
    UNION ALL SELECT * FROM TBLTHREE;

    It said to duplicate the UNION ALL SELECT FROM TBLTHREE and follow with the ;

    I would also like to have WHERE include, such as


    WHERE FIELD1 is NULL for all the tables. Then I'll duplicate the query WHERE FIELD1 is NOT NULL.

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

  3. #3
    jasonbarnes is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2010
    Location
    NC
    Posts
    56
    My problem is doing a union on three or four tables. I don't see this code anywhere.

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by jasonbarnes View Post
    My problem is doing a union on three or four tables. I don't see this code anywhere.
    It would simply be something like:

    Code:
    Select * FROM Table1 WHERE FieldA='SomethingHere'
    UNION ALL
    SELECT * FROM Table2 WHERE FieldA='SomethingHere'
    UNION ALL
    SELECT * FROM Table3 WHERE FieldX='SomethingElseHere'
    Now you can use the * for each of them PROVIDED that you have the exact same number of fields in each of the tables and the fields are in the same order if you want the same data together (in Access it would put all of the data together in the same column even if it is different types, but it would use a string for that and it wouldn't be what you would likely want. In SQL Server this would throw an error because they have to not only have the same field count, but it also needs to have the same datatypes for those same columns). Otherwise you would have to use something like this to account for differences in column count.

    Code:
    Select FIELD1, FIELD2, FIELD3, FIELDA FROM Table1 WHERE Field3='SomethingHere'
    UNION ALL
    SELECT MyField, ThatField, AnotherField, Null  FROM Table2 WHERE AnotherField='SomethingHere'
    UNION ALL
    SELECT Null, ThatField, FIELD3, FIELDA FROM Table3 WHERE FieldA='SomethingElseHere'
    And you do NOT need the semi-colon at the end for Access.

  5. #5
    jasonbarnes is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2010
    Location
    NC
    Posts
    56
    I think part of the problem was the semi-colon. The big one was my larger fields weren't large enough in some of the tables to trigger the memo field. All of this data is imported and I generally use the types access creates. Once I made sure all the types/columns matched it worked fine.

    Thanks for the help!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Union or better way.
    By kevin28 in forum Access
    Replies: 2
    Last Post: 09-06-2011, 02:42 PM
  2. Help with a Union Query
    By Bear in forum Queries
    Replies: 12
    Last Post: 08-14-2011, 05:12 PM
  3. Union Queries
    By tarhim47 in forum Queries
    Replies: 6
    Last Post: 05-26-2011, 12:20 PM
  4. Union ALL problem
    By witooldas in forum Queries
    Replies: 1
    Last Post: 04-29-2011, 07:12 AM
  5. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums