0

我有以下查询:

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 列名“名称”重复

当我回显 my$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 代码以使该查询正常工作?

4

2 回答 2

0

我可以将您的 SQL 更改为以下 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

于 2011-09-05T08:55:11.757 回答
0

当您执行joins 时,您需要告诉它不要从连接表中选择列。

例如:

->join('publishers', 'products.uid_publisher = publishers.uid', **''**)
于 2011-09-05T13:34:40.453 回答