ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 求数据交集的问题

求数据交集的问题

原创 Linux操作系统 作者:space6212 时间:2019-06-18 08:39:05 0 删除 编辑

表结构如下:

SQL> desc item_tag
Name Type Nullable Default Comments
------------------- ---------- -------- ------- --------
ITEM_TAG_ID NUMBER(18)
ITEM_TAG_SEQ_NUMBER NUMBER(16) 9999999
TAG_ID NUMBER(16)
CREATE_DATE DATE SYSDATE
ITEM_ID NUMBER(18)

其中:TAG_ID和ITEM_ID组合建立了一个唯一性索引

求交集的三种方法:


1、

select item_id from item_tag where tag_id = 23468
intersect
select item_id from item_tag where tag_id = 37484
intersect
select item_id from item_tag where tag_id = 37495
intersect
select item_id from item_tag where tag_id = 37223

2、

select it.item_id from Item_Tag it where tag_id=23468 and it.item_id in
(select it.item_id from Item_Tag it where tag_id=37484 and it.item_id in
(select it.item_id from Item_Tag it where tag_id=37495 and it.item_id in
(select it.item_id from Item_Tag it where tag_id=37223
)
)
)

3、

select it.item_id from Item_Tag it where it.tag_id in (23468,37484,37495,37223)
group by it.item_id having count(it.item_id)=4

三者效率上差不多,但第三种方法最简洁,特别是在IN里的列表项很多的时候。

但第三种方法只能用于ITEM_ID和TAG_ID组合是唯一的情况

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/231499/viewspace-63702/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 物化视图
下一篇: 求主外键的关系
请登录后发表评论 登录
全部评论

注册时间:2005-01-25

  • 博文量
    245
  • 访问量
    210062