Solved

basic query access not in

Posted on 2013-12-18
10
343 Views
Last Modified: 2013-12-18
I have a simple DB with 2 tables (all and exclude), in both tables is an "accountname" field.

I need to run a select * from all where the accountname filed is not equal to the accountname field in the exclude table?

Can anyone give me a heads up on the SQL to acheive something like this in access 2010? None of the join options seemed appropriate in design view.
0
Comment
Question by:pma111
  • 5
  • 5
10 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39726762
Try this:

SELECT a.* 
FROM [All] a LEFT JOIN [Exclude] e ON a.accountname = e.accountname
WHERE e.accountname IS NULL

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 39726791
Is it definately a left join because that query has ran ok but returned all results where the exclude entries are also in the output, when I need to cut down the results to those which dont include the exclude acctnames.
0
 
LVL 3

Author Comment

by:pma111
ID: 39726793
or perhaps the = is wrong? should it be not =
0
 
LVL 3

Author Comment

by:pma111
ID: 39726827
basically if table e includes user 7 , user 32 and user 33

but table a contains user 1... user 100

The output of the query should list all users except user 7, user 32 and user 33.

like a select all rows unless the username is included in the list of usernames in the exlude table.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39726843
Can you post the query exactly as you implemented it in your database?

The LEFT JOIN, combined with the IS NULL in the WHERE clause is the basic setup for an 'unmatched query'.... in this case the goal was to select all records in table a that did not have a matching record in table b (which lists names to exclude)
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 3

Author Comment

by:pma111
ID: 39726859
SELECT a.*
FROM [mailboxACL] a LEFT JOIN [exclude] e ON a.Identity = e.acctname
WHERE e.acctname IS NULL

The above has returned entries in the identity column that are entries in the "e.acctname" in the other table.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39726868
An alternative approach  would be:

SELECT a.* 
FROM [All] a 
WHERE a.accountname NOT IN (SELECT e.accountname FROM [Exclude] e)

Open in new window


But the LEFT JOIN method if we can get that working would likely have better performance than the NOT IN method.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39726885
What are the datatypes for Identity and acctname in their respective table designs?
0
 
LVL 3

Author Comment

by:pma111
ID: 39726888
worked, was my mistake (uising the wrong field )

thanks
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39726907
Glad to help out :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Read about achieving the basic levels of HRIS security in the workplace.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now