Solved

writing SQL in Access 2010

Posted on 2016-08-01
11
49 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 49

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 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 38

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 38

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

626 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