Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a one to one left outer join?

Tags:

I was wondering, is there a way to make a kind of one to one left outer join:

I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, but there exists only 1 record that matches that condition, so when it has found its pair on B, it must stop and continue with the next row at table A.

What I have is a simple LEFT OUTER JOIN.

select * from A left outer join B on A.ID = B.ID order by (NAME) asc

Thanks in advance!

like image 346
Sheldon Avatar asked Nov 09 '09 20:11

Sheldon


People also ask

How do you create an outer join?

Double-click the line representing the join you want to change. In the Join Properties dialog box, to create an outer join, select the second or third option. Then, select OK. Remember to pay attention to which table is the base table—the one from which all records are displayed.

Can you have two left outer JOINs?

Yes, it is possible. We would use a query with two LEFT OUTER JOINs to retrieve the hierarchy. The relationships were "zero or more" and it's the zero that tips us off to the need for an OUTER join.

How do you perform a left join in Excel?

In the Merge dialog box, under Right table for merge, select Countries. In the Sales table, select the CountryID column. In the Countries table, select the id column. In the Join kind section, select Left outer.


1 Answers

SQL doesn't work this way. In the first place it does not look at things row-by-row. In the second place what defines the record you want to match on?

Assuming you don't really care which row is selcted, something like this might work:

SELECT * 
From tableA
left outer join 
(select b.* from tableb b1
join (Select min(Id) from tableb group by id) b2 on b1.id - b2.id) b
on a.id = b.id

BUt it still is pretty iffy that you wil get the records you want when there are multiple records with the id in table b.

like image 166
HLGEM Avatar answered Nov 15 '22 06:11

HLGEM