How can I get the first non-empty-value of a group in MySQL?
I have the following table:
id | item_id | name | description | locale
----------------------------------------------------------
1 | 1 | (null) | (null) | en-GB
2 | 1 | Foo | (null) | en-US
3 | 1 | Bar | Lorem ipsum dolor. | en-AU
I'm trying to return a result like the following?:
item_id | name | description
-------------------------------------------------------------------
1 | Foo | Lorem ipsum dolor.
I am told coalsece
was the way to go but it doesn't seem to play with groups. Although the shot didn't fire correctly. It should give a good picture of what I'm trying to do.
select item_id, coalesce(name) as name, coalesce(description) as description from test
where item_id = 1
group by item_id
order by field(locale, 'en-GB', 'en-US', 'en-AU');
Other attempts where made using first(), first_value(), min(), max()... I rather accomplish this in a straight MySQL query than mix it with PHP logic.