您的位置:软件首页 / 数据库 / SQL语句里集合操作

SQL语句里集合操作

ZDNet 软件频道 更新时间:2007-09-14 作者:余 枫 来源:http://fengyu.china.com

本文关键词:微软 SQL dbf文件 代码检查

学数学的时候都知道有交集和差集的概念。

用SQL语句来表示交集和差集的时候会遇到很多小困难,比如条件有重复,编号有空等等。

我最近在帮同事处理一些歌曲列表是否有版权的时候就遇到过这些困难,现在把它们记录一下:

copyright_songs表是有版权的歌曲清单 local_songs表是在用的歌曲清单

生成歌曲名和歌手名都相同的交集用如下SQL:

select distinct t2.id,t1.name,t1.singer,t1.id as local_id,t1.company,
t2.company as copyright_company,t2.type
into same_1
from copyright_songs t2,local_songs t1
where (t1.name=t2.name and t1.singer=t2.singer)

如果歌曲名称里面带有括号,如:星语心愿(节奏版)需要去掉歌曲里面括号后再比较.

select * into local_songs_old from local_songs

update local_songs set name=left(name,charindex('(',name)-1)
where charindex('(',name)>0

drop table same_1

select distinct t2.id,t1.name,t1.singer,t1.id as local_id,t1.company,
t2.company as copyright_company,t2.type
into same_1
from copyright_songs t2,local_songs t1
where (t1.name=t2.name and t1.singer=t2.singer)

显示歌曲名和歌手名都相同歌曲清单(交集)

select * from same_1

显示歌曲名和歌手名都相同歌曲清单,用原来带括号的歌曲名

select distinct t1.id,t2.name,t1.singer,t1.local_id,t1.company,t1.num,t1.type
from same_1 t1,local_songs_old t2
where t1.t1_id=t2.id

找到没有版权对应的歌曲列表

显示编号是否有为空的记录

select * from local_songs where id is null

select * from same_1 where local_id is null

去掉编号为空的记录

delete from local_songs where id is null

delete from same_1 where local_id is null

生成本地歌曲表里面没有版权的歌曲清单(差集)

select * into no_local_songs from local_songs
where id not in (select local_id from same_1)

显示本地歌曲表里面没有版权的歌曲清单

select * from no_local_songs

没有版权的歌曲就要屏蔽起来,不能再使用原来免费的互联网音乐来赢利了。

用户评论

  • 用户名
  • 评论内容
技术关注
当前技术类目:
其它数据库本类技术关注比例:
14软件关注排行:141
36企业级技术关注度:36--

其它数据库技术相关文章:

文章类型收录数量
11 其它数据库的解决方案:
5 其它数据库的成功案例:

软件频道 微软 最新报道

软件频道 SQL 最新报道

软件频道 dbf文件 最新报道

软件频道 代码检查 最新报道

爱卡汽车网 | CNET科技资讯网 | CWEEK | 蜂鸟网 | GameSpot China | 个人电脑 | 开发者在线 | PChome | Solidot | SPN |
投影顾问网 | 万维家电网 | 网友世界 | 西域IT | ZDNet China | 中关村在线 | 中小企业成长网
CNET Networks
Copyright © 1997-2007 CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号