Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with a Left Join

Status
Not open for further replies.

modalman

Programmer
Feb 14, 2001
156
GB
Hi. I am using ASP to query an access database. So far I have used left join to join table2 to table1 where table2.fld1=table1.fld1. That works fine. The SQL is:

SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.fld1=tbl2.fld1

The problem I have is that I only want to use certain records from tbl2. So tbl2 needs to be left joined to tbl1 where fld1 is identical but also where tbl2.fld2 equals a certain value. Any help will be aprreciated.
Many thanks in advance. Modalman

ASCII silly question, get a silly ANSI
 
Thanks Mike. I did try that and it filtered all the records in the recordset. The where clause seemed to execute after the join has been made removing records from tbl1 as well as tbl2. I did place the where clause at the end of the sql statement above. Is it possible that it should be placed elsewhere in the statement?


ASCII silly question, get a silly ANSI
 
Table 2 field 1 is NULL when they are not equal.


SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.fld1=tbl2.fld1
and (tbl2.fld1 IS NULL and tbl1.fld1 = "something")
 
Meant OR.

SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.fld1=tbl2.fld1
OR (tbl2.fld1 IS NULL and tbl1.fld1 = "something")
 
Hi modalman,

You are right about the WHERE - it filters after the JOIN, but you should be able to restrict the records brought into the join like this ..

SELECT * FROM tbl1 LEFT JOIN tbl2 ON (tbl1.fld1 = tbl2.fld1 AND tbl2.fld2 = "yourvalue");

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top