2011-09-04 3 views
0
에이 쿼리를 얻는 방법

나는 다음과 같은 쿼리가 :Zend_db_select

$selectProducts = $db->select() 
     ->from('products', array('products.uid AS id', 
      'products.title', 
      'products.ean', 
      'products.description', 
      'products.price', 
      'products.date_publish', 
      'publishers.name', 
      'GROUP_CONCAT(CONCAT (authors.first_name, \' \', authors.last_name) SEPARATOR \', \') AS authors')) 
     ->join('publishers', 'products.uid_publisher = publishers.uid') 
     ->join('products_authors_mm', 'products.uid = products_authors_mm.uid_product') 
     ->join('authors', 'products_authors_mm.uid_author = authors.uid') 
     ->group('products.uid'); 

$finalSelect = $db->select() 
       ->from($selectProducts) 
       ->where('t.title LIKE ?', '%' . $query . '%') 
       ->orWhere('t.name LIKE ?', '%' . $query . '%') 
       ->orWhere('t.authors LIKE ?', '%' . $query . '%'); 

MYSQL이 나에게 다음과 같은 오류를 제공합니다 : 나는 그런 zend_db_select 쿼리에 넣어하려고하면

SELECT * 
FROM 
(SELECT products.uid, products.title, products.ean, products.description, products.price, products.date_publish, publishers.name, GROUP_CONCAT(CONCAT (authors.first_name, ' ', authors.last_name) SEPARATOR ', ') AS authors 
FROM products 
INNER JOIN publishers 
ON products.uid_publisher = publishers.uid 
INNER JOIN products_authors_mm 
ON products.uid = products_authors_mm.uid_product 
INNER JOIN authors 
ON products_authors_mm.uid_author = authors.uid 
GROUP BY products.uid) AS t 
WHERE title LIKE '%jerzy%' OR name LIKE '%jerzy%' OR authors LIKE '%jerzy%' 

를 :

Message: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'name'

$finalSelect 에코 때, 나는이 쿼리를 얻을 :

SELECT `t`.* 
FROM 
(SELECT `products`.`uid` AS `id`, `products`.`title`, `products`.`ean`, `products`.`description`, `products`.`price`, `products`.`date_publish`, `publishers`.`name`, 
GROUP_CONCAT(CONCAT (authors.first_name, ' ', authors.last_name) SEPARATOR ', ') AS `authors`**, `publishers`.*, `products_authors_mm`.*, `authors`.*** 
FROM `products` 
INNER JOIN `publishers` ON products.uid_publisher = publishers.uid 
INNER JOIN `products_authors_mm` ON products.uid = products_authors_mm.uid_product 
INNER JOIN `authors` ON products_authors_mm.uid_author = authors.uid 
GROUP BY `products`.`uid`) AS `t` 
WHERE (t.title LIKE '%Andrzej%') OR (t.name LIKE '%Andrzej%') OR (t.authors LIKE '%Andrzej%') 

거기에서 publishers.*, products_authors_mm.*, authors.* 을 제거하면 올바르게 작동합니다.

제 질문은이 쿼리가 작동하도록 PHP 코드를 어떻게 바꿀 수 있습니까?

답변

0

join을 수행 할 때 조인 된 테이블에서 열을 선택하지 않도록 지시해야합니다.

예를 들어 :

->join('publishers', 'products.uid_publisher = publishers.uid', **''**) 
+0

THX, 난이 세번째 파라미터 잊어 – vthruu

0

다음과 같이 SQL을 변경할 수 있습니까?

SELECT products.uid, 
     products.title, 
     products.ean, 
     products.description, 
     products.price, 
     products.date_publish, 
     publishers.name, 
     GROUP_CONCAT(CONCAT (authors.first_name, ' ', authors.last_name) SEPARATOR ', ') AS authors 
     FROM products 
    INNER JOIN publishers ON products.uid_publisher = publishers.uid 
    INNER JOIN products_authors_mm ON products.uid = products_authors_mm.uid_product 
    INNER JOIN authors ON products_authors_mm.uid_author = authors.uid 
    GROUP BY products.uid 
    WHERE title LIKE '%jerzy%' OR name LIKE '%jerzy%' OR authors LIKE '%jerzy%' 

올바른 경우 다음 zend 쿼리를 사용할 수 있습니다.

$db->select() 
    ->from(array('a' => 'products'), array('uid', 'title', 'ean', 'description', 'price', 'date_publish')) 
    ->join(array('b' => 'publishers'), 'a.uid_publisher = b.uid', array('name')) 
    ->join(array('c' => 'products_authors_mm'), 'a.uid = c.uid_product', '') 
    ->join(array('d' => 'authors'), 'c.uid_author = d.uid', array('GROUP_CONCAT(CONCAT (authors.first_name, ' ', authors.last_name) SEPARATOR ', ') AS authors') 
    ->where('title LIKE %jerzy%') 
    ->orWhere('name LIKE %jerzy%') 
    ->orWhere('authors LIKE %jerzy%') 
    ->group('a.uid'); 

위의 코드를 확인하십시오. authors LIKE %jerzy%'이 (가) 잘 작동하는지 모르겠다. 또한 내 새로운 쿼리는 결과를 제공합니다. : P

관련 문제