Solved

writing SQL in Access 2010

Posted on 2016-08-01
11
46 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 36

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 36

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

821 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