我有以下查询:如何得到这个查询到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代码,使这个查询工作?
THX,我忘了这个第三个参数, – vthruu