簡體   English   中英

如何將查詢查詢到Zend_db_select

[英]how to get this query into 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重復的列名“名稱”

當我呼應$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代碼才能正常工作?

我可以按照以下方式更改您的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%'這樣的authors LIKE %jerzy%'是否工作。 我的新查詢也是給你結果。 :P

當你的join S,你需要告訴它不會從連接表中選擇列。

例如:

->join('publishers', 'products.uid_publisher = publishers.uid', **''**)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM