2

I want to search multi vars that could be true in WHERE condition or in HAVING condition in a group_concat table. Something like this:

SELECT
    article.id,
    article.name,
    GROUP_CONCAT(tags.name order by tags.name) AS nameTags,
    GROUP_CONCAT(tags.id order by tags.id) AS idTags
FROM
    article
    LEFT JOIN ....
    LEFT JOIN ....
WHERE article.name LIKE '%var1%' OR HAVING nameTags LIKE '%var1%' AND
WHERE article.name LIKE '%var2%' OR HAVING nameTags LIKE '%var2%'
GROUP BY id

Is there some way possible or is it better to do 2 querys?

This query:

SELECT
    article.id,
    article.name,
    GROUP_CONCAT(tags.name order by tags.name) AS nameTags,
    GROUP_CONCAT(tags.id order by tags.id) AS idTags
FROM
    article
    LEFT JOIN ....
    LEFT JOIN ....
WHERE
(article.name LIKE '%var1%' OR  tags.name LIKE '%var1%') AND
(article.name LIKE '%var2%' OR  tags.name LIKE '%var2%')
GROUP BY id

Find what I search, but I must do a second query to show results as I want.

sorry if question is repeated, I didn't know how to ask it, so I did'nt know how to search for it. Sorry my english too!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Leo
  • 580
  • 7
  • 22

1 Answers1

0
SELECT
    article.id,
    article.name,
    GROUP_CONCAT(tags.name order by tags.name) AS nameTags,
    GROUP_CONCAT(tags.id order by tags.id) AS idTags,
    MAX(IF(article.name LIKE '%var1%',1,0)) AS var1match,
    MAX(IF(article.name LIKE '%var2%',1,0)) AS var2match
FROM
    article
    LEFT JOIN ....
    LEFT JOIN ....
GROUP BY id
HAVING
    (nameTags LIKE '%var1%' OR var1match=1)
    OR (nameTags LIKE '%var2%' OR var2match=1)
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • It works perfectly! Didn't know this function! I will study to understand it well! I can't give you points yet, sorry, and thanks a lot! – Leo Mar 17 '12 at 21:05
  • Just had to add comma between the MAX itens – Leo Mar 17 '12 at 21:17