View Single Post
Old 11-04-2008, 03:54 AM   #1 (permalink)
benton
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Retrieve from only one table

I'm trying to alter a search function that searches a number of tables for some word. The problem I am having is that the word can exist in more than one table so it is returning duplicate results.

For example, in the pseudo code below, if the name being searched for (sally) appears in tables B or C, then the address from the initial table should be returned. This part works. But if the name is in B and C, then two results are returned. So I need to be able to check the tables B and C with an exclusive or condition.

tableA {
id = 1;
name = sally;
address = 123 Street
}

tableB {
id = 1;
name = sally;
}

tableC {
id = 1;
name = sally;
}

select a.address from
tableA a left join
tableB b on a.id = b.id left join
tableC c on a.id = c.id
where b.name like %sally% or (b.name not like %sally% and c.name like %sally%)

I thought the above would filter out the second occurrence but it still shows up. Can anyone see a way to solve this, please?
benton is offline  
Reply With Quote