Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Combinando duas tabelas com sql JOIN?


Você precisa agrupar por algo ou o agregado GROUP_CONCAT() irá coletar todas as linhas em uma:
GROUP BY  tour_foreign.id 

Usar:
$query = $this -> db -> query('
    SELECT
       @rownum := @rownum + 1 rownum,
       tour_foreign.id, 
       tour_foreign.name, 
       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 . '%" 
        OR tour_foreign_residence.name_re LIKE "%' . $find . '%"
    GROUP BY  tour_foreign.id ');