I want to left join TableA to TableB where a certain condition in tableA is true So I do this type of SQL query
Select * from
TableA Left Join TableB on TableA.fld1 = TableB.fld2
where TableA.fld3 = True
This works OK. Now however I want to do the Join only with certain records in TableB, ie those records in TableB where a certain condition is met, specifically fld4 has false as its value. The reason I want to do this is I want to know which rows in tableA don't have a match in tableB among the rows in tableB where fld4 is false.
If I was to delete all the rows in TableB where fld4 is true and run the above query I'd get the correct result. All I'd need to do is find the rows in the resultant recordset with null in some cell. But if instead of deleting rows from TableB first I alter the query to the one below I get no rows at all returned
Select * from
TableA Left Join TableB on TableA.fld1 = TableB.fld2
where TableA.fld3 = True
and TableB.fld4 = false
If my ramblings make sense can someone tell me what I'm doing wrong? Thanks
The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.
Syntax For Left Join:SELECT column names FROM table1 LEFT JOIN table2 ON table1. matching_column = table2. matching_column; Note: For example, if you have a left table with 10 rows, you are guaranteed to have at least 10 rows after applying join operation on two tables.
The Left Join in SQL basically returns all records from the left table and the matched records from the right tables. For example, let's say, we have two tables, Table A and Table B. When Left Join is applied on these two tables, all records from Table A and only the matched records from Table B will be displayed.
SQL LEFT JOIN examples Each location belongs to one and only one country while each country can have zero or more locations. The relationship between the countries and locations tables is one-to-many.
You should put the condition in the join clause. When you have a where clause that filters rows on the "right" side of a left join query, you ultimately exclude rows. Try this:
Select *
from TableA
Left Join TableB
on TableA.fld1 = TableB.fld2
and TableB.fld4 = false
where TableA.fld3 = True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With