Use of CONCAT, CONCAT_WS and GROUP_CONCAT

Asked

Viewed 370 times

4

I’m building a query where I look for results within the concatenation and get enough to ask for help. I currently use Mysql with the PHP application.

In the example below, I look for the words "F1000" and "GRID" in a column concatenation of both tables. The product may or may not have a grid.

SELECT p.id, 
       p.id_maker, 
       p.name, 
       p.description, 
       p.specifications 
FROM   product AS p 
WHERE  CONCAT(
           CONCAT_WS(
               ' ', 
               p.name, 
               p.description, 
               p.specifications
           ), ', ', 
           GROUP_CONCAT(
              (
                 SELECT CONCAT_WS(
                     ' ', 
                     g.description, 
                     g.original_cod_maker
                 ) 
                 FROM product_grid AS g 
                 WHERE g.fk_son = p.id
           ) 
           SEPARATOR ', ')
       ) 
       REGEXP '(.*)GRID(.*)F1000(.*)'

This query displays error 1111 (invalid use of group Function) concerning the use of concatenation methods.

I thank you in advance for your help.

  • You are on the Portuguese website it was not necessary to translate each paragraph.

1 answer

2


The command GROUP_CONCAT serves to concatenate groups when using aggregation functions such as SUM, MAX, AVG, etc..

For your query, you can only search within the fields directly. A query suggestion:

SELECT p.id, 
       p.id_maker, 
       p.name, 
       p.description, 
       p.specifications 
FROM   product AS p 
LEFT JOIN product_grid AS g
    ON (p.id = g.fk_son)
WHERE (
  p.name               LIKE '%GRID%' OR p.name               LIKE '%F1000%'
  OR
  p.description        LIKE '%GRID%' OR p.description        LIKE '%F1000%'
  OR
  p.specifications     LIKE '%GRID%' OR p.specifications     LIKE '%F1000%'
  OR
  g.description        LIKE '%GRID%' OR g.description        LIKE '%F1000%'
  OR
  g.original_cod_maker LIKE '%GRID%' OR g.original_cod_maker LIKE '%F1000%'
)
  • After a long time, the matter is settled, jlHentel. It took me a while at the time, but I simply put an OR and included practically a CONCAT like the first one, with REGEXP, but without looking for the product in the product_grid table. Finally, I put the two conditions between parentheses. It worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.