Reputation: 9803
I need help writting a query. Using this schema as example:
Table: A
id 1
id 2
Table: ALocalized
A_id 1, culture 1, name 'EN translation'
A_id 1, culture 2, name 'PT translation'
A_id 2, culture 1, name 'EN translation'
This is the schema i'm using to localize information. Each record on A has at least one related record on ALocalized, but there may be some rows who don't have a localized record for certain cultures.
In this example, i want to query A and filter the ALocalized by culture 2. If culture 2 doesn't exist, then return any other available. The result should be:
A_id 1, culture 2, name 'PT translation'
A_id 2, culture 1, name 'EN translation'
I've tried to do this joining both tables, using an sub query but i think that my problem is in where clause.
where (ALocalized.culture = @cultureparameter OR ALocalized.culture = 1 OR ALocalized.culture = 2) limit 1
When @cultureparameter is 2 returns culture 1. It isn't respecting the order of where clauses
How should i do this? I was trying to avoid the use of loops to check each A record for an existence of ALocalized.
Thanks in advance
Upvotes: 1
Views: 896
Reputation: 263723
SELECT a.*,
COALESCE(b.culture, c.culture) Culture,
COALESCE(b.Name, c.Name) Name
FROM tableA a
LEFT JOIN tableB b
ON a.id = b.a_id AND
b.culture = 2
LEFT JOIN tableB c
ON a.id = c.a_id AND
c.culture = 1
Upvotes: 1