Group_CONCAT搜索

时间:2011-10-20 07:23:03

标签: php mysql join group-concat

  

可能重复:
  Combining several database table together?

我有以下查询,想要对$find name_re_all中的每个值进行处理,我的查询中有哪些?

例如: name_re_all如下:

  

ROW 3: 11111 22222 33333 44444
ROW 2: 55555 66666 77777
  ROW 1: 88888 99999 112233 445566

如果值$find11111,则显示所有值第3行:11111 22222 33333 44444

如果值$find66666,则显示所有值第2行:55555 66666 77777

如果值$find33333,则显示所有值第3行:11111 22222 33333 44444

如果值$find778899,则显示所有值第1行:88888 99999 112233 445566

...

$query = $this -> db -> query('
            SELECT
               @rownum := @rownum + 1 rownum,
               tour_foreign.id, 
               tour_foreign.name,
               tour_foreign.airline,
               MIN(tour_foreign_residence.name_re) AS name_re, 
               tour_foreign.service, 
               tour_foreign.date_go, 
               tour_foreign.date_back, 
               tour_foreign.term,
               tour_foreign.useradmin_submit,
               tour_foreign.date_submit,
               GROUP_CONCAT( tour_foreign_residence.name_re 
                     ORDER BY tour_foreign_residence.name_re 
                     SEPARATOR "، "
                   ) AS name_re_all
            FROM   tour_foreign 
              INNER JOIN tour_foreign_residence 
            ON ( tour_foreign.id = tour_foreign_residence.relation )
              JOIN (SELECT @rownum := 0) r
            WHERE  tour_foreign.name LIKE "%' . $find . '%" 
            GROUP BY  tour_foreign.id 
            HAVING name_re_all LIKE "%' . $find . '%"'
            );

我在数据库中的表:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

作为变体:将此查询包装到子查询 -

SELECT * FROM (
  your query...
) t
WHERE FIND_IN_SET('11111', name_re_all);

修改 你应该使用子查询!

SELECT * FROM (
  SELECT
     @rownum := @rownum + 1 rownum,
     ...
     ...
     ...
     GROUP_CONCAT( tour_foreign_residence.name_re 
           ORDER BY tour_foreign_residence.name_re 
           SEPARATOR "، "
         ) AS name_re_all
  FROM   tour_foreign 
    INNER JOIN tour_foreign_residence 
  ON ( tour_foreign.id = tour_foreign_residence.relation )
    JOIN (SELECT @rownum := 0) r
  WHERE  tour_foreign.name LIKE "%' . $find . '%" 
  GROUP BY  tour_foreign.id 
--  HAVING name_re_all LIKE "%' . $find . '%"
) t
WHERE FIND_IN_SET('11111', name_re_all)