如何将此查询放入 Zend_db_select

发布于 2024-12-03 04:37:37 字数 2637 浏览 0 评论 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.* 时,作者。* 从那里开始,它工作得很好。

所以我的问题是,如何更改 PHP 代码才能使该查询正常工作?

I have the following query:

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%'

When I try to put it into a zend_db_select query like that:

$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 gives me the following error:

Message: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'name'

When I echo my $finalSelect, I get this query:

    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%')

When I remove publishers.*, products_authors_mm.*, authors.*
from there, it works fine.

So my question is, how can I change my PHP code for this query to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-12-10 04:37:37

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

例如:

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

When you do the joins, you need to tell it not to select the columns from the joined table.

For example:

->join('publishers', 'products.uid_publisher = publishers.uid', **''**)
夜无邪 2024-12-10 04:37:37

我可以将您的 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

Can I change your SQL as following one.

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%'

if it is correct you can use following zend query.

$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');

Please check above code. I don't know authors LIKE %jerzy%' is work or not. Also my new query is give your result. :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文