Results 1 to 5 of 5
  1. #1
    Bobby100 is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    13

    Select from a second table if first select returns nothing

    Hi,



    I have been trying to work out how to create a SELECT query that if it returns nothing will then perform a second SELECT query on a second table.

    So Select idnumber, age from TABLE_A where idnumber = 100

    if that id number is not in TABLE_A then perform..

    SELECT idnumber, age from TABLE_B where idnumber = 100

    I tried to write this in SQL but it didn't work. You can't start a SQL query with an IIF statement? i.e. IIF(select query is null, perfrom second query, else perform first query)?

    Can you write and save the first query then reference this in the second query in design view, so that you use the first query as the criteria?

    Any help would be appreciated.

    Thanks.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    you can't do it as two separate queries except through vba but it might work as a single query - all depends on the data

    if idnumber is in either table but not both - use a union query
    if idnumber could be in either or both with the same age - use a union query and then group by or distinct to eliminate duplicates
    if idnumber could be in both but age could be different - you need a combination of union queries and left joins plus a decision process of which to take when different - tableA or tableB or highest or lowest

  3. #3
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You can't start a SQL query with an IIF statement?
    No, you cannot start a query with any function reference.
    Can you write and save the first query then reference this in the second query in design view
    What if it returns no records?
    The OP reads like the db is not normalized; or is there another reason why you'd find similar records in more than one table (aside from being in separate db's?).
    Anyway, even if "idnumber could be in either or both with the same age - use a union query" I suspect a union query alone would suffice - just don't use the "All" keyword.
    I think if you have this
    tblA tblB
    ID Age ID Age
    100 10 100 40
    100 20
    100 30
    100 40
    that this
    SELECT ID, Age from tblA WHERE ID = 100
    UNION
    SELECT ID, Age from tblB WHERE ID = 100
    will give you this
    ID Age
    100 10
    100 20
    100 30
    100 40
    without getting two records for 100 / 40. If you use the All keyword, you will get 2 records for 100 / 40.
    Last edited by Micron; 09-06-2016 at 10:22 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    good point micron - had forgotten that was the default position. A while since I've used a union query!

  5. #5
    Bobby100 is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    13
    Thanks guys,

    Some very helpful information.

    I will try out the union query as suggested.

    IN answer to your question.. yes the database is not normalized.

    That would be the optimal solution.. to normalize the db, but this is outside of my remit at present.

    Thanks.

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

Similar Threads

  1. SELECT * vs. SELECT SomeField... which is faster?
    By MatthewGrace in forum Queries
    Replies: 5
    Last Post: 12-18-2015, 08:57 PM
  2. Replies: 5
    Last Post: 05-14-2014, 01:17 PM
  3. SELECT TOP 10 Query returns 12 records
    By Paul H in forum Queries
    Replies: 8
    Last Post: 09-11-2013, 03:38 PM
  4. Replies: 5
    Last Post: 07-26-2012, 12:59 PM
  5. SELECT returns more than one row
    By 83dons in forum Queries
    Replies: 1
    Last Post: 01-13-2010, 11:28 AM

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
  •  
Other Forums: Microsoft Office Forums