请教这个SQL该怎么写?

Kevin_Lmx 2004-09-07 04:31:40
有三个表,分别保存客户信息(Cust),模块信息(Module),客户模块的更新信息(Renewal):
客户表(Cust)有以下两个字段:
cid --客户编号
cname --客户名
模块表(Module)有以下两个字段:
id --id号
module --模块名
更新表(Renewal)有以下两个字段:
cid --客户编号,对应Cust.cid
mid --模块编号, 对应Module.id

问题如下:
搜索出(任一或全部,可以在前台程序中控制的)客户未更新的模块名称。如何用一段SQL语句来实现?
...全文
242 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-09-08
  • 打赏
  • 举报
回复
--如果要求查询的结果集字段保持一致

--查询的存储过程
create proc p_qry
@cname varchar(100)='' --客户名
as
if isnull(@cname,'')=''
--搜索出全部客户未更新的模块名称。
select *
from Module a,Cust b
where not exists(
select * from Renewal where mid=a.id and cid=b.cid)
order by b.cid

else
--搜索出任一客户未更新的模块名称。
select *
from Module a,Cust b
where b.cname=@cname and not exists(
select * from Renewal where mid=a.id and cid=b.cid)
go
zjcxc 元老 2004-09-08
  • 打赏
  • 举报
回复
--测试

--测试数据
create table Cust(cid int,cname varchar(10))
insert Cust select 1,'梅州'
union all select 2,'五羊'

create table Module(id int,module varchar(10))
insert Module select 1,'a'
union all select 2,'b'

create table Renewal(cid int,mid int)
insert Renewal select 1,1
union all select 1,2
union all select 2,2
go

--查询的存储过程
create proc p_qry
@cname varchar(100)='' --客户名
as
if isnull(@cname,'')=''
--搜索出全部客户未更新的模块名称。
select *
from Module a,Cust b
where not exists(
select * from Renewal where mid=a.id and cid=b.cid)
order by b.cid

else
--搜索出任一客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal r,(select cid from Cust where cname=@cname) c
where mid=a.id and r.cid=c.cid)
go

--调用
exec p_qry
go

--删除测试
drop table Module,Renewal,Cust
drop proc p_qry

/*--测试结果

id module cid cname
----------- ---------- ----------- ----------
1 a 2 五羊

(所影响的行数为 1 行)
--*/
zjcxc 元老 2004-09-08
  • 打赏
  • 举报
回复
--那就是这样修改
--查询的存储过程

create proc p_qry
@cname varchar(100)='' --客户名
as
if isnull(@cname,'')=''
--搜索出全部客户未更新的模块名称。
select *
from Module a,Cust b
where not exists(
select * from Renewal where mid=a.id and cid=b.cid)
order by b.cid

else
--搜索出任一客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal r,(select cid from Cust where cname=@cname) c
where mid=a.id and r.cid=c.cid)
go
zjcxc 元老 2004-09-08
  • 打赏
  • 举报
回复
那应该是理解上出了问题.

搜索出全部客户未更新的模块名称


我的理解是没有任何客户更新过该模块
看楼主举的例子,是说要按客户来分类,显示出每个客户没有更新过的模块吧?
Kevin_Lmx 2004-09-08
  • 打赏
  • 举报
回复
现在在module表中存在两个模块,分别是a,b
在客户表中存在两个客户:‘梅州’ 和‘五羊’
‘梅州’更新了‘a'和‘b’,而‘五羊’更新了‘a’,
应该可以查找出‘五羊’未更新‘b’,
但是查不出来。请帮忙
长风大叔 2004-09-08
  • 打赏
  • 举报
回复
select * from module a where a.id not in (select distinct mid from renewal)
zjcxc 元老 2004-09-08
  • 打赏
  • 举报
回复
举数据实例说明,
Kevin_Lmx 2004-09-08
  • 打赏
  • 举报
回复
查询结果还是不对
zjcxc 元老 2004-09-07
  • 打赏
  • 举报
回复
--或者写成存储过程
create proc p_qry
@cname varchar(100)='' --客户名
as
if isnull(@cname,'')=''
--搜索出全部客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal where mid=a.id)

else
--搜索出任一客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal r,(select cid from Cust where cname=@cname) c
where mid=a.id and r.cid=c.cid)
zjcxc 元老 2004-09-07
  • 打赏
  • 举报
回复
--搜索出任一客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal r,(select cid from Cust where cname='客户名') c
where mid=a.id and r.cid=c.cid)
zjcxc 元老 2004-09-07
  • 打赏
  • 举报
回复
--搜索出全部客户未更新的模块名称。
select *
from Module a
where not exists(
select * from Renewal where mid=a.id)

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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