Thanks again.
I have all the vendor info in one table, so I replaced 'Child' by 'Vendor' again in your sql.
So, even though this works great, there are cases when some vendors do not have a parent or any children. Is it possible to inject that condition into this sql so that one sql can bring out just the parent vendors, those with some child vendors or no child? I have used the following sql just to extract such vendors:
select distinct p.id, p.name, r.child_id from vendor p left join relationship r on r.child_id = p.id where r.child_id is null and p.name like '%eg%'
nordborg wrote:
In this case using joins may produce duplicates of the vendor if the filter matches more than one child. It is possible to remove the duplicates by using the
distinct keyword. Eg.
Code:
select distinct v.id, v.name from Vendor v
join Relatinship r on r.par_id = v.id
join Child c on c.id = r.child_id
where c.name = 'a_ge'
Annother possibility is to use subselects:
Code:
select v.id, v.name from Vendor v
where v.id in (
select r.par_id from Relatinship r
join Child c on c.id = r.child_id
where c.name = 'a_ge'
)