sql - Mysql group_concat() re-orders resultset -
sql - Mysql group_concat() re-orders resultset -
consider mysql schema this:
create table `translations` ( group_id int(11), lang varchar(2), text varchar(9), unique index `group_id_lang` (`group_id`, `lang`) )
it should quite obvious going on here - have many translations 1 group_id.
now if i'll run next query, groups preferred language 'en', fallback 'ru', , if neither of these translations exists, fallback available language, works charm
select * ( select * translations order lang = 'en' desc, lang = 'ru' desc ) translations grouping group_id +----------+------+---------+ | group_id | lang | text | +----------+------+---------+ | 1 | en | republic of estonia | | 2 | en | england | | 3 | ru | Швеция | +----------+------+---------+
as see i'll 3 rows lastly 1 fallen 'ru' because 'en' wasn't nowadays on group. far good..
now if want select available languages column adding group_concat(lang) translations
, things bit fuzzy:
select *, group_concat(lang) available_translations ( select * translations order lang = 'en' desc, lang = 'ru' desc ) translations grouping group_id +----------+------+-----------+--------------+ | group_id | lang | text | translations | +----------+------+-----------+--------------+ | 1 | et | eesti | et,en,ru | | 2 | et | inglismaa | et,en,ru | | 3 | ru | Швеция | ru,et | +----------+------+-----------+--------------+
now mysql doesn't respect order by
clause, , groups in native order there's no ordering @ all.
why group_concat
causing issue?
how can create work have preferred languages , list available translations row?
also, if you'd fiddle around here go
the order of results not guaranteed until outer query, though have ordered subquery, not guaranteed order records read, , returned outer query. main issue though, abusing mysql grouping extension allows select columns not contained in grouping clause or aggregate function.
to simplify query have:
select group_id, text translations grouping group_id order lang = 'en' desc, lang = 'ru' desc
unless text
functionally dependant on group_id
breaks sql standard, nonetheless, mysql allows it, however, stated in mysql documents
the server free take value each group, unless same, values chosen indeterminate. furthermore, selection of values each grouping cannot influenced adding order clause.
so though have order not apply until after 1 row per grouping has been selected, , 1 row non-determistic. doing ordering final result set.
in order work around , determinsitic results, can concatenate texts using group_concat
, explicitly state required order:
group_concat(text order lang = 'en' desc, lang = 'ru' desc)
then utilize substring_index
extract first term. query becomes:
select group_id, substring_index( group_concat(lang order lang = 'en' desc, lang = 'ru' desc), ',', 1) lang, substring_index( group_concat(text order lang = 'en' desc, lang = 'ru' desc), ',', 1) text, group_concat(lang order lang = 'en' desc, lang = 'ru' desc) available_translations translations grouping group_id;
example on sql fiddle
mysql sql group-by sql-order-by
Comments
Post a Comment