原则 2 自连接

发布于 2024-12-10 10:34:26 字数 1877 浏览 0 评论 0原文

我想生成一个复杂的自连接查询抛出DQL。

这是我的表结构的简单版本。

CREATE TABLE `item_view` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_item` int(11) NOT NULL,
  `id_user` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
) 

实体将是

    /**
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;


     /**
     * @ManyToOne(targetEntity="User")
     * @JoinColumns({
     *   @JoinColumn(name="id_user", referencedColumnName="id_user")
     * })
     */
    private $user;

    /**
   * @ManyToOne(targetEntity="Item")
     * @JoinColumns({
     *   @JoinColumn(name="id_item", referencedColumnName="id_item")
     * })
     */
    private $item;

我非常想生成这样的查询:(

select 
    `c2`.*, count(`c1`.`id_user`) as `vc`
from
    `item_view` `c1`
        inner join
    `item_view` `c2` ON `c1`.`id_user` = `c2`.`id_user`
where
    `c1`.`id_item` in (6) and `c2`.`id_item` not in (6)
group by `c2`.`id_item`
order by `vc` desc

通过这个本机查询我可以选择常见的用户信息。

知道我们如何才能抛出DQL吗?

更新

我刚刚找到一个解决方法是抛出一个桥接关系

SELECT v,COUNT(v.user) AS USR_CNT FROM ItemView v JOIN v.user u JOIN u.views v2 
WHERE v2.item IN (:items) and v NOT IN (:items)
GROUP BY v.item
ORDER BY USR_CNT DESC

,该关系将生成此 SQL

SELECT 
    `i0_`.`id` AS `id0`,
    COUNT(`i0_`.`id_user`) AS `sclr2`,
    `i0_`.`id_user` AS `id_user3`,
    `i0_`.`id_item` AS `id_item4`
FROM
    `item_view` `i0_`
        INNER JOIN
    `user` `u1_` ON `i0_`.`id_user` = `u1_`.`id_user`
        INNER JOIN
    `item_view` `i2_` ON `u1_`.`id_user` = `i2_`.`id_user`
WHERE
    `i2_`.`id_item` IN (6) AND `i0_`.`id` NOT IN (6)
GROUP BY `i0_`.`id_item`
ORDER BY `sclr2` DESC

那么我们如何消除与用户表的额外连接?!

I want to generate a complex self join Query throw DQL.

here is a simple version of my table structure.

CREATE TABLE `item_view` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_item` int(11) NOT NULL,
  `id_user` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
) 

and the Entity will be

    /**
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;


     /**
     * @ManyToOne(targetEntity="User")
     * @JoinColumns({
     *   @JoinColumn(name="id_user", referencedColumnName="id_user")
     * })
     */
    private $user;

    /**
   * @ManyToOne(targetEntity="Item")
     * @JoinColumns({
     *   @JoinColumn(name="id_item", referencedColumnName="id_item")
     * })
     */
    private $item;

I am dying to generate a query like this one :(

select 
    `c2`.*, count(`c1`.`id_user`) as `vc`
from
    `item_view` `c1`
        inner join
    `item_view` `c2` ON `c1`.`id_user` = `c2`.`id_user`
where
    `c1`.`id_item` in (6) and `c2`.`id_item` not in (6)
group by `c2`.`id_item`
order by `vc` desc

by this native query I can select common user information.

any idea how we can do that throw DQL?

UPDATE

I just find a work around throw a bridge relation

SELECT v,COUNT(v.user) AS USR_CNT FROM ItemView v JOIN v.user u JOIN u.views v2 
WHERE v2.item IN (:items) and v NOT IN (:items)
GROUP BY v.item
ORDER BY USR_CNT DESC

which will generate this SQL

SELECT 
    `i0_`.`id` AS `id0`,
    COUNT(`i0_`.`id_user`) AS `sclr2`,
    `i0_`.`id_user` AS `id_user3`,
    `i0_`.`id_item` AS `id_item4`
FROM
    `item_view` `i0_`
        INNER JOIN
    `user` `u1_` ON `i0_`.`id_user` = `u1_`.`id_user`
        INNER JOIN
    `item_view` `i2_` ON `u1_`.`id_user` = `i2_`.`id_user`
WHERE
    `i2_`.`id_item` IN (6) AND `i0_`.`id` NOT IN (6)
GROUP BY `i0_`.`id_item`
ORDER BY `sclr2` DESC

So how can we eliminate the extra join with the user table ?!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文