If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL Left join with select criteria

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-05, 14:42
doze doze is offline
Registered User
 
Join Date: Jul 2004
Posts: 19
SQL Left join with select criteria

Hey, maybe I posted this to the wrong section first, but here:

http://www.dbforums.com/microsoft-access/1117963-sql-left-join-problem.html#post4168806

I got a problem with creating a sql query from multiple tables and use one table as left join. I get "syntax error in join operation".
Reply With Quote
  #2 (permalink)  
Old 02-16-05, 15:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Look at the suggestion from RedNeckGeek. You can't mix SQL-89 with SQL-92 syntax in Jet (the database engine used by MS-Access).

-PatP
Reply With Quote
  #3 (permalink)  
Old 02-17-05, 01:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
pat, if by "mix SQL-89 with SQL-92 syntax" you're referring to the comma-delimited list of tables (implicit inner or cross join) with JOIN syntax, then yes, you can

the syntax error comes from the fact that access requires joins to be parenthesized if there are more than two tables involved
Code:
  from (
       (
       (
       hiihtokerta
inner 
  join hiihtaja 
    on hiihtokerta.hiihtaja_id = hiihtaja.hiihtaja_id
       )
inner 
  join suksi 
    on hiihtokerta.suksi_id = suksi.suksi_id
       )
inner 
  join voide 
    on hiihtokerta.voide_id = voide.voide_id
       )
left outer
  join tavoite 
    on (
       hiihtokerta.paivamaara = tavoite.paivamaara 
   and hiihtokerta.hiihtaja_id = tavoite.hiihtaja_id
       )
 where ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-17-05, 07:31
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
Quote:
Originally Posted by r937
the syntax error comes from the fact that access requires joins to be parenthesized if there are more than two tables involved
Rudy,
That isn't necessarily true. By default, when using the query builder, Access will insert all those parentheses. However, the query will run just fine when you delete them. In most of the SQL i generate using VBA, I don't bother with them. The solution I supplied to Doze worked for me the way I built it,
using my own data in a similar manner to what he was doing.
__________________
Inspiration Through Fermentation
Reply With Quote
  #5 (permalink)  
Old 02-17-05, 07:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
hmm, that's a new one on me, thanks

perhaps they took that requirement out in a very recent release of Access

i know it to be a requirement in at least Access 2000
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 02-17-05, 08:32
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The MS-Access GUI generates more parenteses than a LISP program, but Jet hasn't needed them for at least a couple of years. I don't use Jet a lot, but I've never tried to mix SQL-89 syntax (comma separated tables in the FROM, join criteria in the WHERE) with SQL-92 or later syntax (using the JOIN ... ON syntax in the FROM only).

It might well be possible, but I've never gotten it to work.

-PatP
Reply With Quote
  #7 (permalink)  
Old 02-17-05, 11:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Pat Phelan
The MS-Access GUI generates more parenteses than a LISP program, but Jet hasn't needed them for at least a couple of years.
as i said, maybe a very recent version of access has dropped this requirement but it is still necessary in Access 2000

create these three tables in access:

pat1
id descr
1 foo
2 bar
3 qux

pat2
id descr
1 one
2 two
3 three

pat1pat2
pat1id pat2id
1 2
1 3
2 1
2 3
3 1
3 2
3 3

then try running this query:
Code:
select pat1.descr as pat1descr
     , pat2.descr as pat2descr
  from pat1 
inner 
  join pat1pat2 
    on pat1.id = pat1pat2.pat1id
inner
  join pat2
    on pat1pat2.pat2id = pat2.id
syntax error (missing operator) in query expression

however, if you add the parentheses:
Code:
select pat1.descr as pat1descr
     , pat2.descr as pat2descr
  from (
       pat1 
inner 
  join pat1pat2 
    on pat1.id = pat1pat2.pat1id
       )
inner
  join pat2
    on pat1pat2.pat2id = pat2.id
then tada, nice results:

pat1descr pat2descr
foo two
foo three
bar one
bar three
qux one
qux two
qux three

explanation? parentheses are required for multiple joins!!!

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-17-05, 16:02
doze doze is offline
Registered User
 
Join Date: Jul 2004
Posts: 19
Quote:
Originally Posted by r937
the syntax error comes from the fact that access requires joins to be parenthesized if there are more than two tables involved
Yep, that did it for me and now it works great! thanks alot guys for all the help!


Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On