2011-09-04 81 views
0

我有以下查询:如何得到这个查询到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%' 

当我试图把它变成一个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给我下面的错误:

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