I have a table with columns "tag" and "category". Tags can contain spaces, but I want to filter them using a query string that has a dash instead of spaces (it comes from the URL pool).
I run the following query in mysql:
SELECT GROUP_CONCAT(tag SEPARATOR ',' ) AS tags FROM tags GROUP BY category HAVING REPLACE( tags, ' ', '-' ) like "%a%"
Result:
first-tag,second-tag third-tag,fourth-tag fifth-tag
Can someone explain why returned tags also have dashes? I would expect (and hope) that REPLACE will be used to filter by the HAVING statement, but the original value that will be returned by the request. How can i achieve this?
Here is the script for your reference: http://sqlfiddle.com/#!2/d7573/1
Thanks!
EDITOR (by gordon):
For those who are interested, this problem occurs in this simpler version of the request:
SELECT GROUP_CONCAT(tag) AS tags FROM tags HAVING REPLACE(tags, ' ', '-') is not null;
As Rustzhvan notes, this is a reference problem. This does not happen with group_concat() directly in having .
Sasha source share