Solved

writing SQL in Access 2010

Posted on 2016-08-01
11
41 Views
Last Modified: 2016-08-03
hello!
I have two tables in Access 2010 let's say table1 and table2. Both tables have field 1, field 2, and field 3 that are common fields. I want to query all the records in table 1 where field 1, field 2, and field 3 exist in table2. So basically something like

Select * from table1 where table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3

let's say I want to find all the records in table 1 where name, phone, and address all exist in table 2. I just don't know how to write that exactly. Thanks!
0
Comment
Question by:mrosier
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 41738127
To locate all rows that match via the  common fields you use an INNER JOIN, like this:
Select * 
from table1 
inner join table2 on  table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3

Open in new window

Once you have that join in place you an also add further filtering conditions in the where clause like this. Note the logic you require MIGHT be better using OR's instead of AND's in the tht where clause, you may have to experiment with those.
Select * 
from table1 
inner join table2 on  table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3
where table1.name= table2.name
and table1.phone=table2.phone
and table1.address=table2.address

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 41738149
In case if it complains, you can enclose the JOIN in parentheses.
SELECT * 
FROM   (table1 
        INNER JOIN table2 
                ON table1.field1 = table2.field2 
                   AND table1.field2 = table2.field2 
                   AND table1.field3 = table2. field3) 

Open in new window

0
 
LVL 34

Expert Comment

by:PatHartman
ID: 41738252
Paul is on the right track.  If you want exact matches on all three fields, use his first example.  No WHERE clause is necessary.  If you want to get partial matches, then use a cross join  with OR as the relational operator for the WHERE clause.  But in the case where you are using the "OR", you will need to select the relevant columns from BOTH tables.

Select table1.*, table2.*
from table1, table2
where table1.name= table2.name
OR table1.phone=table2.phone
OR table1.address=table2.address
0
 

Author Comment

by:mrosier
ID: 41738953
Thanks folks! So if in the results I get for this query, if I delete records, it only deletes from table1 since I am only pulling FROM table1, correct?
0
 

Author Comment

by:mrosier
ID: 41739052
The query worked great, I managed to query all the records I wanted to query. I tried deleting them from table1 as that is my ultimate goal, but Access wouldn't allow that. I worked around it via using the unique IDs of these records, created a new table with these results, and just deleted from table1 using referencing this table. Is there an easy way to perform a delete function using this same query though?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 41739181
You didn't say which query you used.  The cross join query is not updateable so you wouldn't be able to delete from either table.

In a query that joins multiple tables, you can only delete from the lowest level "child" table.  These tables do not have relationships defined so you probably won't be able to delete from either table since Access won't be able to determine the relationship to know which table is the "child".  

Therefore, you are going to have to identify the records using one of these queries and then save the IDs you want to delete into a temp table and use that to control the deleting.
0
 

Author Comment

by:mrosier
ID: 41739184
Oh I used Paul's query, and thanks for the info Pat!
0
 

Author Closing Comment

by:mrosier
ID: 41739188
Paul had a typo in his query, but I believe his intentions should be clear. He had said table2.field2 at the start of the join when it should be table2.field1
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41739824
inner join table2 on  table1.field1 = table2.field2

mea culpa that was an error, it should have been

          inner join table2 on  table1.field1 = table2.field1

(in my defense I was answering using a phone bouncing about in a train :-)

Thanks.
0
 

Author Comment

by:mrosier
ID: 41740580
Wow thanks Paul! I think EE should get that train moment on video and pay you for an advertisement emphasizing troubleshooting from anywhere :p
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41741267
..grin..
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

760 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

17 Experts available now in Live!

Get 1:1 Help Now