求一条SQL语句写法

honlon 2008-10-27 01:34:16
表结构为:

ArticleID Keyword
1 不堪一击
1 绝非
1 中国
2 计划
2 空军
2 马公
2 美国
2 战机
3 大盘
3 基金

每个文章在表中会有若干条关键字记录。

问题,如何查询出关键字有5个相同的文章来。
...全文
250 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 huowang 的回复:]
SQL codeselectdistincta.ArticleIDasone, b.ArticleIDastwofromarticle ainnerjoinarticle bona.keyword=b.keywordanda.articleid<>b.articleidgroupbya.ArticleID,b.ArticleIDhavingcount(b.ArticleID)>4

没有测试,你试试!
[/Quote]
这个好像还可以
等不到来世 2008-10-27
  • 打赏
  • 举报
回复
晕,结得好快。。。
create table tb(ArticleID int,    Keyword nvarchar(100))
insert tb select
1 , '关键字1' union select
1 , '关键字2' union select
1 , '关键字3' union select
1 , '关键字4' union select
1 , '关键字5' union select
2 , '关键字1' union select
2 ,'关键字2' union select
2 ,'关键字3' union select
2 ,'关键字4' union select
2 ,'关键字5' union select
3 , '关键字1' union select
3 ,'关键字2' union select
3 ,'关键字3' union select
3 ,'关键字4' union select
3 ,'关键字5' union select
4 , '关键字1' union select
4 ,'关键字2' union select
4 ,'关键字3' union select
5 , '关键字1' union select
5 ,'关键字2'


select a.ArticleID,SimilarArticleID=b.ArticleID
from (select distinct ArticleID from tb) a join (select distinct ArticleID from tb) b
on a.ArticleID<b.ArticleID
and (select count(distinct Keyword) from tb where ArticleID=b.ArticleID and Keyword in(select Keyword from tb where ArticleID=a.ArticleID))=5

/*
ArticleID SimilarArticleID
----------- ----------------
1 2
1 3
2 3

*/

drop table tb
huowang 2008-10-27
  • 打赏
  • 举报
回复
select distinct a.ArticleID as one,  b.ArticleID as two 
from article a inner join article b on a.keyword=b.keyword and a.articleid<>b.articleid
group by a.ArticleID,b.ArticleID
having count(b.ArticleID)>4


没有测试,你试试!
honlon 2008-10-27
  • 打赏
  • 举报
回复
哎,还是程序里处理,结贴了
等不到来世 2008-10-27
  • 打赏
  • 举报
回复
…………
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 szx1999 的回复:]
改进了一下方法:
SQL codecreatetabletb(ArticleIDint, Keywordnvarchar(100))inserttbselect1,'关键字1'unionselect1,'关键字2'unionselect1,'关键字3'unionselect1,'关键字4'unionselect1,'关键字5'unionselect2,'关键字1'unionselect2,'关键字2'unionselect2,'关键字3'unionselect2,'关键字4'unionselect2,'关键字5'unionselect3,'关键字1'unionselect3,'关键字2'unionselect3,'关键字3'unionselect3,'关键字4'unionsele…
[/Quote]
不行,sql语句执行了2分钟了还没反应,Management Studio卡的动不了了
等不到来世 2008-10-27
  • 打赏
  • 举报
回复
改进了一下方法:
create table tb(ArticleID int,    Keyword nvarchar(100))
insert tb select
1 , '关键字1' union select
1 , '关键字2' union select
1 , '关键字3' union select
1 , '关键字4' union select
1 , '关键字5' union select
2 , '关键字1' union select
2 ,'关键字2' union select
2 ,'关键字3' union select
2 ,'关键字4' union select
2 ,'关键字5' union select
3 , '关键字1' union select
3 ,'关键字2' union select
3 ,'关键字3' union select
3 ,'关键字4' union select
3 ,'关键字5' union select
4 , '关键字1' union select
4 ,'关键字2' union select
4 ,'关键字3' union select
5 , '关键字1' union select
5 ,'关键字2'


select distinct a.ArticleID,SimilarArticleID=b.ArticleID
from tb a,tb b
where a.ArticleID<b.ArticleID
and (select count(distinct Keyword) from tb where ArticleID=b.ArticleID and Keyword in(select Keyword from tb where ArticleID=a.ArticleID))=5

/*
ArticleID SimilarArticleID
----------- ----------------
1 2
1 3
2 3

(3 row(s) affected)

(1 row(s) affected)
*/


select * from tb
drop table tb
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 tangsong78 的回复:]
其实我三楼就说过了,用fetch可以处理。

具体的输出肯定是要你自己定义的。

结果楼主回我一个“汗了”。

有点伤心啊。
[/Quote]
我去试试你的方法 ^-^
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 tangsong78 的回复:]
其实我三楼就说过了,用fetch可以处理。

具体的输出肯定是要你自己定义的。

结果楼主回我一个“汗了”。

有点伤心啊。
[/Quote]
不好意思,我没联系上下文看,所以没看到你的意思,抱歉了 -_-!
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 dawugui 的回复:]
引用 20 楼 honlon 的回复:
引用 18 楼 dawugui 的回复:
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.

是呀,但是就是想查出来关键字相同的文章来。看来还是得用c#来处理了

如果是这个要求,只能使用循环(一般来说是用程序)来逐一判断1和2,1和3,2和3...是否有五个(或五个以上)相同的.
[/Quote]
不过在往这个表插入数据的时候,一般都限制一个文章的关键字数量在5条,所以说一个文章在这个表记录最多有5条。
用程序判断的话先用开始的和后面的依次比较,找到匹配的话就将匹配的记录下来,然后以后判断的时候就不用再去
判断这些已将找到的。不知道这样的方法是否可行?
underlemontree 2008-10-27
  • 打赏
  • 举报
回复

declare @t table
(
ArticleID int,
Keyword varchar(20)
)

insert @t select 1,'不堪一击'
insert @t select 1,'绝非'
insert @t select 1,'不堪一击'
insert @t select 1,'不堪一击'
insert @t select 1,'不堪一击'
insert @t select 1,'不堪一击'
insert @t select 2,'空军'
insert @t select 2,'空军'
insert @t select 2,'马公'
insert @t select 2,'美国'
insert @t select 2,'战机'
insert @t select 3,'大盘'
insert @t select 3,'基金'
李冬宝 2008-10-27
  • 打赏
  • 举报
回复
其实我三楼就说过了,用fetch可以处理。

具体的输出肯定是要你自己定义的。

结果楼主回我一个“汗了”。

有点伤心啊。
underlemontree 2008-10-27
  • 打赏
  • 举报
回复

select distinct ArticleID from @t where Keyword=(select Keyword from @t group by Keyword having count(*)=5)
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 szx1999 的回复:]
SQL codecreatetabletb(ArticleIDint, Keywordnvarchar(100))inserttbselect1,'关键字1'unionselect1,'关键字2'unionselect1,'关键字3'unionselect1,'关键字4'unionselect1,'关键字5'unionselect2,'关键字1'unionselect2,'关键字2'unionselect2,'关键字3'unionselect2,'关键字4'unionselect2,'关键字5'unionselect3,'关键字1'unionselect3,'关键字2'unionselect3,'关键字3'selectdistinctArticleID,SimilarArticleIDfrom(sele…
[/Quote]

消息 512,级别 16,状态 1,第 1 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
dawugui 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 honlon 的回复:]
引用 18 楼 dawugui 的回复:
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.

是呀,但是就是想查出来关键字相同的文章来。看来还是得用c#来处理了
[/Quote]
如果是这个要求,只能使用循环(一般来说是用程序)来逐一判断1和2,1和3,2和3...是否有五个(或五个以上)相同的.
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 szx1999 的回复:]
引用 18 楼 dawugui 的回复:
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.

用偶的语句都能查出来,木哈哈哈~
结果是按article大小排列,把满足要求的文章一对一对的列出来。
[/Quote]
谢谢,我试试看
honlon 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 dawugui 的回复:]
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.
[/Quote]
是呀,但是就是想查出来关键字相同的文章来。看来还是得用c#来处理了
等不到来世 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 dawugui 的回复:]
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.
[/Quote]
用偶的语句都能查出来,木哈哈哈~
结果是按article大小排列,把满足要求的文章一对一对的列出来。
dawugui 2008-10-27
  • 打赏
  • 举报
回复
如果1和2有五个相同的,2和3有另外五个相同的,这样子的话就很麻烦了.
除非指定五个具体的值,然后来查询.
等不到来世 2008-10-27
  • 打赏
  • 举报
回复
create table tb(ArticleID int,    Keyword nvarchar(100))
insert tb select
1 , '关键字1' union select
1 , '关键字2' union select
1 , '关键字3' union select
1 , '关键字4' union select
1 , '关键字5' union select
2 , '关键字1' union select
2 ,'关键字2' union select
2 ,'关键字3' union select
2 ,'关键字4' union select
2 ,'关键字5' union select
3 , '关键字1' union select
3 ,'关键字2' union select
3 ,'关键字3'

select distinct ArticleID,SimilarArticleID from
(
select ArticleID,SimilarArticleID=(select ArticleID from tb t1 where ArticleID>t.ArticleID and Keyword in (select Keyword from tb where ArticleID=t1.ArticleID) group by ArticleID having count(*)=5)
from tb t
) t2
where SimilarArticleID is not null

/*
ArticleID SimilarArticleID
----------- ----------------
1 2

(1 row(s) affected)
*/

加载更多回复(16)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧