OK, so this one has had me ripping my hair out for hours. I feel like there is something obvious I am overlooking.
I have 2 tables, service and brand
service
-------
id
brand
brand
-----
id
brandName
So service.brand can be any of these:
Blank
"Other"
Integer (matches brand.id)
String (matches brand.brandName)
String (Not Blank, Not Other, Not brand.brandName)
I'm trying to write a query that will pull up the correct brandname from the brand table, and if the value of service.brand is not in brand.id or brand.brandName, then display whatever is there.
So far I got everything working except it wouldn't pull up the record if service.brand was Not Blank, Not Other, Not in brand.id, Not in brand.brandName. (which I'm calling OtherThanOther from here on out).
Now my latest attempt pulls up mostly correct, but the OtherThanOther field gets pulled up many times, like if total records is 40, OtherThanOther is the same record almost 20 times. HELP!
My latest attempt..
select
s.*, b.brandName as bname
from
service s, brand b
where
s.brand = b.brandName
or
s.brand = b.id
or
s.brand = 'Other'
or
s.brand = ''
or
(
s.brand not in (select brandName from brand)
and
s.brand not in (select id from brand)
and
s.brand != 'Other'
and
s.brand != ''
)
Sample Table Data
service
-------
1 5
2 Dell
3 SomeRandom
4
5 Other
brand
-----
1 HP
2 Gateway
3 Dell
4 Compaq
5 Toshiba
my query results..
(service.id, service.brand, brand.id, brand.brandName, bname)
-------------------------------------------------------------
1 5 5 Toshiba Toshiba
2 Dell 3 Dell Dell
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
4 '', null, null, null
5 Other, null, null, null
I need it to just pull SomeRandom once, group by won't work because there may be multiple fields with the same value as SomeRandom. Any help is much appreciated.
Thanks!