Results 1 to 5 of 5

Thread: Selecting data based on 2 tables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Location
    NH
    Posts
    90

    Selecting data based on 2 tables

    What I am trying to do is to write a query that finds all records in Table ADS that do not have a corrisponding record in the tables PROOFS. Help

    SELECT *
    FROM Ads
    WHERE AdNums NOT IN
    (SELECT Proofs.AdNums
    FROM proofs))

    Ads
    AD_ID Long
    RevisionDate Date
    AdNums long

    Proofs
    Ad_ID
    .....
    AdNums long


    Ads

  2. #2
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Your query should be working. What problems are you having?
    live, code and die...

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try this...
    Code:
    SELECT *
    FROM Ads
    WHERE Ads.AdNums NOT EXISTS
    (SELECT Proofs.AdNums
    FROM proofs))
    From SQL BOL:

    When a subquery is introduced with the keyword EXISTS, it
    functions as an existence test. The WHERE clause of the outer
    query tests for the existence of rows returned by the subquery.
    The subquery does not actually produce any data; it returns a
    value of TRUE or FALSE.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Originally posted by RobDog888
    Try this...
    Code:
    SELECT *
    FROM Ads
    WHERE Ads.AdNums NOT EXISTS
    (SELECT Proofs.AdNums
    FROM proofs))
    This will not work. What slr have should be working, if we want to try your code, we should do this:
    Code:
    SELECT *
    FROM Ads
    WHERE NOT EXISTS
    (SELECT Proofs.AdNums
    FROM proofs WHERE Ads.AdNums = Proofs.AdNums)
    Also, I suggest that you name your columns differently next time. Say Ads.AdNums, Proofs.RefAdNums.

    Also (again), this might just be typo error but check your code again:
    Originally posted by slr
    What I am trying to do is to write a query that finds all records in Table ADS that do not have a corrisponding record in the tables PROOFS. Help

    SELECT *
    FROM Ads
    WHERE AdNums NOT IN
    (SELECT Proofs.AdNums
    FROM proofs))

    Ads
    AD_ID Long
    RevisionDate Date
    AdNums long

    Proofs
    Ad_ID
    .....
    AdNums long


    Ads
    you have unmatched parenthesis, again, it might just be typo error (here in the forum), but it won't work in your query.
    live, code and die...

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    SELECT *
    FROM Ads
    WHERE AdNums NOT IN
    (SELECT Proofs.AdNums
    FROM proofs))
    Code:
    Select * from Ads left join AdNums on ads.[idfield]=adnums.[idfield] where adnums.[idfield] is null
    id field in ads and in adnums are the two related fields.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width