如何将此查询放入 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 给出以下错误:
消息:SQLSTATE[42S21]:列已存在:1060 重复的列名“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.* 时,作者。*
从那里开始,它工作得很好。
所以我的问题是,如何更改 PHP 代码才能使该查询正常工作?
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:
$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:
Message: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'name'
When I echo my $finalSelect
, I get this query:
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.
So my question is, how can I change my PHP code for this query to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行
join
时,您需要告诉它不要从连接表中选择列。例如:
When you do the
join
s, you need to tell it not to select the columns from the joined table.For example:
我可以将您的 SQL 更改为以下之一吗?
如果正确,您可以使用以下 zend 查询。
请检查上面的代码。我不知道
authors LIKE %jerzy%'
是否有效。我的新查询也是给出你的结果。 :PCan I change your SQL as following one.
if it is correct you can use following zend query.
Please check above code. I don't know
authors LIKE %jerzy%'
is work or not. Also my new query is give your result. :P