I was under the impression that in MySQL, GROUP BY supported the use of column names or column alias'.
I have some fairly simple SQL which is doing a cross join between 2 tables and then a left join to get some matching data (if it exists), and concatenating up matching data. This was giving strange results. Finally realised that the problem appears to be my use of alias names rather than the actual column names.
With alias names it seems to have treated those lines rows with a non matched row on the left join differently. Hence getting 2 rows returned by the group by despite sharing the same GROUP BY fields.
The 2 queries are, firstly grouping by column alias':-
SELECT a.id AS item_id, a.code AS item_code, GROUP_CONCAT( CONCAT_WS( ":", c.name, IFNULL( d.value, "void" ) )
ORDER BY c.name ) AS AllAnaly
FROM item a
CROSS JOIN item_analy c
LEFT OUTER JOIN item_analy_value d ON a.id = d.item_id
AND c.id = d.analy_id
GROUP BY item_id, item_code;
and grouping by column names
SELECT a.id AS item_id, a.code AS item_code, GROUP_CONCAT( CONCAT_WS( ":", c.name, IFNULL( d.value, "void" ) )
ORDER BY c.name ) AS AllAnaly
FROM item a
CROSS JOIN item_analy c
LEFT OUTER JOIN item_analy_value d ON a.id = d.item_id
AND c.id = d.analy_id
GROUP BY a.id, a.code;
I have knocked up an SQL fiddle that demonstrates this:-
http://sqlfiddle.com/#!2/35a69
I am missing something with how MySQL treats alias' in GROUP BY clauses?