mysql - Symfony2 查询在两个表上使用左连接

标签 mysql symfony join doctrine-orm left-join

我正在尝试从查询中检索产品信息,该查询在一个表中查找标识符并在另一个表中匹配该标识符以获取产品 ID,然后在产品表中查找以获取产品信息。由于某种原因,我无法成功运行此查询。任何帮助我将不胜感激!!!

 $query = $em->createQuery('
            SELECT p
            FROM WIC\ProductBundle\Entity\Product p
            LEFT JOIN WIC\ListingBundle\Entity\Listing l
                ON l.product = p.id
            LEFT JOIN WIC\ListingBundle\Entity\ListingAma la
                ON la.id = l.id
            WHERE la.standardProductIdValue LIKE :stringValue
            AND   p.account = :account_id')
            ->setMaxResults(10)
            ->setParameter('stringValue', "%1234567890%")
            ->setParameter('account_id', $account_id); 

产品表

 |id|product_name      |account_id|
 ----------------------------------
 |1|Test Product 1     |74        | 
 |2|Test Product 2     |74        | 
 |3|Test Product 3     |74        | 
 |4|Test Product 4     |74        | 
 |5|Test Product 5     |74        | 

list 表

 |id|product_id       |
 ----------------------
 |1|1                 |
 |2|1                 |
 |3|2                 |
 |4|3                 |
 |5|1                 |
 |6|3                 |
 |7|5                 |

ListingAma 表

 |id|standardProductIdValue |
 ----------------------------
 |1|1234567890              |
 |2|1234567890              |
 |3|AAAAAAAAAA              |
 |4|BBBBBBBBBB              |
 |5|1234567890              |
 |6|CCCCCCCCCC              |
 |7|0000000000              |

Listing 和 ListingAma 表是基类 (Listing) 和扩展类 (ListingAma),因此它们共享每个表中的 ID 号。

我想要返回 standardProductIdValue 等于“1234567890”的任何查询的产品信息。在这种情况下,它应该检索行 1,2 和 5,然后在列表中它应该将其与产品 id #1 同步,并仅返回“测试产品 1”。

上面的查询在 Symfony 中创建了此错误:

 [Syntax Error] line 0, col 163: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON' (500 Internal Server Error)

感谢您的帮助!!!

最佳答案

尝试使用此查询:

    $query = $em->createQuery("
        SELECT p
        FROM WIC\ProductBundle\Entity\Product p
        LEFT JOIN WIC\ListingBundle\Entity\Listing l
            ON l.product = p.id
        LEFT JOIN WIC\ListingBundle\Entity\ListingAma la
            ON la.id = l.id
        WHERE la.standardProductIdValue LIKE :stringValue")
        ->setMaxResults(10)
        ->setParameter("stringValue", "%1234567890%"); 

这是否也失败并出现相同的错误?

关于mysql - Symfony2 查询在两个表上使用左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341565/

相关文章:

MySQL,联接多对多关系查询

mysql - 连接表中选定数据的查询计数

forms - 带有 optgroup 的 Symfony2 选择字段中的实体映射

phpunit + symfony 错误处理程序已更改

php - Twig 忘记了数组键

php - 如何将一个表元素与另一表进行比较

c# - 使用正则表达式连接 linq

mysql - SQL SELECT 不是数字但可以为 NULL

php - 如何在 PhalconPHP 中实现单个表列的多个条件

mysql - 帮助优化这个mysql查询