原则 2 自连接
我想生成一个复杂的自连接查询抛出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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论