我有以下查询:
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.*, authors.*
时
从那里,它工作正常。
所以我的问题是,如何更改我的PHP代码以使此查询起作用?
答案 0 :(得分: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
答案 1 :(得分:0)
当你执行join
时,你需要告诉它不要从连接表中选择列。
例如:
->join('publishers', 'products.uid_publisher = publishers.uid', **''**)