Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

writing SQL in Access 2010

Posted on 2016-08-01
11
Medium Priority
?
55 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 49

Accepted Solution

by:
PortletPaul earned 1000 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 41

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 40

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
LVL 40

Assisted Solution

by:PatHartman
PatHartman earned 1000 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 49

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 49

Expert Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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