[英]how to get this query into Zend_db_select
I have the following query: 我有以下查询:
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%'
When I try to put it into a zend_db_select
query like that: 当我尝试将其放入
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 gives me the following error: MYSQL给我以下错误:
Message: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'name'
消息:SQLSTATE [42S21]:列已存在:1060重复的列名“名称”
When I echo my $finalSelect
, I get this query: 当我呼应
$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%')
When I remove publishers.*, products_authors_mm.*, authors.*
from there, it works fine. 当我从那里删除
publishers.*, products_authors_mm.*, authors.*
时,它可以正常工作。
So my question is, how can I change my PHP code for this query to work? 所以我的问题是,如何更改此查询的PHP代码才能正常工作?
Can I change your SQL as following one. 我可以按照以下方式更改您的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%'
if it is correct you can use following zend query. 如果正确,则可以使用以下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');
Please check above code. 请检查上面的代码。 I don't know
authors LIKE %jerzy%'
is work or not. 我不知道
authors LIKE %jerzy%'
这样的authors LIKE %jerzy%'
是否工作。 Also my new query is give your result. 我的新查询也是给你结果。 :P
:P
When you do the join
s, you need to tell it not to select the columns from the joined table. 当你的
join
S,你需要告诉它不会从连接表中选择列。
For example: 例如:
->join('publishers', 'products.uid_publisher = publishers.uid', **''**)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.