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重複する列名'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コードを変更するにはどうすればよいですか?

4

2 に答える 2

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

于 2011-09-05T08:55:11.757 に答える
0

を実行するときはjoin、結合されたテーブルから列を選択しないように指示する必要があります。

例えば:

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