【问题标题】:how to get this query into Zend_db_select如何让这个查询进入 Zend_db_select
【发布时间】:2011-09-04 20:00:41
【问题描述】:

我有以下疑问:

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 Duplicate column name '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 代码以使该查询正常工作?

【问题讨论】:

    标签: php mysql zend-db-select


    【解决方案1】:

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

    例如:

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

    【讨论】:

      【解决方案2】:

      我能把你的 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        • 2019-11-02
        • 1970-01-01
        相关资源
        最近更新 更多