gpt4 book ai didi

mysql - 多对多 + WHERE 字段 <> 值

转载 作者:行者123 更新时间:2023-11-29 13:47:49 25 4
gpt4 key购买 nike

我有“Post”实体,其关系为manyToMany

 /**
* @ORM\ManyToMany(targetEntity="Core\CoreBundle\Entity\User", cascade={"persist", "remove", "merge"})
* @ORM\JoinTable(name="blogregion_post_views")
*/
protected $views;

/**
* @ORM\ManyToMany(targetEntity="Application\BlogRegionBundle\Entity\PostTag", cascade={"persist", "remove", "merge"})
* @ORM\JoinTable(name="blogregion_tags_match")
*/
protected $tags;

我想获得新帖子

    $query = $this->getEm()->createQuery("
SELECT p FROM BlogRegionBundle:Post p LEFT JOIN p.tags t LEFT JOIN p.views v
WHERE t.id = :blogId AND p.state = :state AND p.deleted = 0 AND (v <> :user OR v IS NULL )

")
->setParameter('blogId', $blogId)
->setParameter('state', 1)
->setParameter('user', $this->getUser())
;

但如果我的用户存在于“views”关系中,它会返回“Post”

如何解决这个问题?

最佳答案

您可以尝试使用 MEMBER OF 条件。

SELECT p FROM BlogRegionBundle:Post p LEFT JOIN p.tags t WHERE t.id = :blogId AND p.state = :state AND p.deleted = 0 AND :user NOT MEMBER OF p.views

来自http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples

关于mysql - 多对多 + WHERE 字段 <> 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17157846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com