这样的sql语句怎么写?帮帮忙……

HappyFay 2010-05-18 06:07:36
现在在做的一个项目要完成的功能很简单,可还是在SQL语句这里难住了,请大家帮忙解决一下,先谢了。。。。。。

这里有三个表,表 RS_Rule 是所有的Rule 信息,RS_SureRule 是所有确认过的 Rule信息,还有一个User 的用户表。

RS_SureRule:
SureUserId SureRuleId Update_Time Update_Host
9007 311      2010/5/13 16:45 DV-LIN
9007 313      2010/5/13 18:03 DV-LIN
9005 314      2010/5/13 18:08 DV-LIN
9005 308      2010/5/13 14:25 DV-LIN
9005 312      2010/5/13 14:31 DV-LIN
9006 311      2010/5/13 14:32 DV-LIN
9009 312      2010/5/13 14:33 DV-LIN
NULL NULL NULL NULL

RS_Rule :
Id Title Text Creater Updater
311 a abc 9005 9009
312 b bdf 9006 9007
313 c fag 9005 9005
314 d gaf 9006 9008


RS_User:
UserId UserName
9005 Fay
9006 Betty
9007 Ninocle
9008 Lucy
9009 Lily

现在我想知道未确认的Rule信息,应该怎么写SQL呢?

不好意思,刚开始学这个,不太清楚……请大家帮帮忙咯。

...全文
158 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
xz_lm_fly 2010-05-21
  • 打赏
  • 举报
回复

WITH StateCTE(UserId,State,Time) AS
(
SELECT SureUserId,'Yes',Update_Time FROM RS_SureRule WHERE SureRuleId = '311'
)
SELECT * FROM StateCTE
UNION
SELECT UserId,'No',NULL FROM RS_User WHERE UserId NOT IN(SELECT UserId FROM StateCTE)
ORDER BY UserId
HappyNicole 2010-05-21
  • 打赏
  • 举报
回复
呜呜……


偶不知道额。。。


呵呵 帮顶帮顶!!!
HappyFay 2010-05-20
  • 打赏
  • 举报
回复
5555555555……

怎么没有人帮我嘞?

大家帮忙看看哈……

永生天地 2010-05-19
  • 打赏
  • 举报
回复
就是这个吗?

select * from RS_Rule where id not in(select SureRuleId from RS_SureRule where SureUserId='9005')

/*
Id Title Text Creater Updater
------------ ---------- -------------------------------------------------- ------------ ------------
311 a abc 9005 9009
313 c fag 9005 9005

(所影响的行数为 2 行)

*/
HappyFay 2010-05-19
  • 打赏
  • 举报
回复
呵呵,真的太谢谢你们了……

我先试试看吧。。。

lolylyx 2010-05-19
  • 打赏
  • 举报
回复
jaydom是正确的,not in 换成not exists效率会好点
lolylyx 2010-05-19
  • 打赏
  • 举报
回复
汗~看错了,我以为SureUserId是组合的
lolylyx 2010-05-19
  • 打赏
  • 举报
回复
select * from RS_Rule a where not exists(select 1 from RS_SureRule b where a.Id = substring(b.SureUserId,5,3))

最好不要用not in
没验证过,你试下
jaydom 2010-05-19
  • 打赏
  • 举报
回复

use PracticeDB
go
if exists (select 1 from sysobjects where name in ('RS_SureRule'))
drop table RS_SureRule
go
create table RS_SureRule
(
SureUserId numeric(10),
SureRuleId numeric(10),
Update_Time datetime,
Update_Host varchar(50)
)
go
insert into RS_SureRule
select 9007 ,311 ,'2010/5/13 16:45','DV-LIN' union all
select 9007 ,313 ,'2010/5/13 18:03','DV-LIN' union all
select 9005 ,314 ,'2010/5/13 18:08','DV-LIN' union all
select 9005 ,308 ,'2010/5/13 14:25','DV-LIN' union all
select 9005 ,312 ,'2010/5/13 14:31','DV-LIN' union all
select 9006 ,311 ,'2010/5/13 14:32','DV-LIN' union all
select 9009 ,312 ,'2010/5/13 14:33','DV-LIN'
go
create table RS_Rule
(
Id numeric(10),
Title varchar(10),
Text varchar(50),
Creater numeric(10),
Updater numeric(10)
)
go
insert into RS_Rule
select 311, 'a' ,'abc' ,9005 ,9009 union all
select 312, 'b' ,'bdf' ,9006 ,9007 union all
select 313, 'c' ,'fag' ,9005 ,9005 union all
select 314, 'd' ,'gaf' ,9006 ,9008
go
create table RS_User
(
UserId numeric(10),
UserName varchar(50)
)
go
insert into RS_User
select 9005, 'Fay' union all
select 9006, 'Betty' union all
select 9007, 'Ninocle' union all
select 9008, 'Lucy' union all
select 9009, 'Lily'

select top 3* from RS_Rule
select top 3* from RS_SureRule
select top 3* from RS_User


select *
from RS_Rule r
where r.Id not in(select r.Id
from RS_Rule r join RS_SureRule s on r.Id=s.SureRuleId
left join RS_User u on s.SureUserId=u.UserId
where u.UserName='Fay')
hbjlwhl 2010-05-19
  • 打赏
  • 举报
回复
帮楼主顶一下!!!!
HappyFay 2010-05-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 happyfay 的回复:]
现在在做的一个项目要完成的功能很简单,可还是在SQL语句这里难住了,请大家帮忙解决一下,先谢了。。。。。。

这里有三个表,表 RS_Rule 是所有的Rule 信息,RS_SureRule 是所有确认过的 Rule信息,还有一个User 的用户表。

RS_SureRule:
SureUserId SureRuleId Update_Time Update_Host
9007 ……
[/Quote]



首先用户先登录进入Rule管理页面,查看未确认的Rule,则会显示当前用户没有看过的Rule信息

如 UserId 为9005的用户Fay 没有确认过的RuleId就是311 , 313 ;9007的用户Ninocle 没有确认过的RuleId 就是 312 , 314 , 308

最后用户Fay登陆以后他没有确认的Rule显示出来的结果就是:

RuleId Title Text Creater Updater
311 a abc 9005 9009
313 c fag 9005 9005

不知道这样说的请不清楚。。。谢谢你哈

weiki516 2010-05-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jaydom 的回复:]
SQL code

select *
from RS_Rule
where id not in (select SureRuleId
from RS_SureRule)
[/Quote]
2楼正解
HappyFay 2010-05-19
  • 打赏
  • 举报
回复
呵呵,不管怎么样,谢谢了……
yewen360281 2010-05-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yewen360281 的回复:]
SQL code

select * from RS_Rule as a,RS_SureRule as b, User as c where not b.SureRuleId=a.Id and b.SureUserId=c.UserId




不晓得是对还是错~!!
我没测的~!!!
[/Quote]
应该是错的,误人子弟啊
HappyFay 2010-05-19
  • 打赏
  • 举报
回复
恩。谢谢大家了,这个问题解决了。。。还有一个也请大家帮忙看看啊……接着上面的表

在"Rule 确认状况" 这个页面中需要显示确认人 确认状态 确认时间三列。

比如,id 为311 的Rule确认状况 就应该是:

User State Time
Fay NO
Betty YES 2010/5/13 14:32
Ninocle YES 2010/5/13 16:45
Lucy NO
Lily NO

大家再帮帮忙啊,一定给分……
HappyFay 2010-05-19
  • 打赏
  • 举报
回复
恩。谢谢大家了,这个问题解决了。。。还有一个也请大家帮忙看看啊……接着上面的表

在"Rule 确认状况" 这个页面中需要显示确认人 确认状态 确认时间三列。

比如,id 为311 的Rule确认状况 就应该是:

User State Time
9005 NO
9006 YES 2010/5/13 14:32
9007 YES 2010/5/13 16:45
9009 NO

大家再帮帮忙啊,一定给分……
jaydom 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jaydom 的回复:]
SQL code

select *
from RS_Rule
where id not in (select SureRuleId
from RS_SureRule)
[/Quote]
是这样吗
jaydom 2010-05-18
  • 打赏
  • 举报
回复

select *
from RS_Rule
where id not in (select SureRuleId
from RS_SureRule)
htl258_Tony 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 happyfay 的回复:]
现在在做的一个项目要完成的功能很简单,可还是在SQL语句这里难住了,请大家帮忙解决一下,先谢了。。。。。。

这里有三个表,表 RS_Rule 是所有的Rule 信息,RS_SureRule 是所有确认过的 Rule信息,还有一个User 的用户表。

RS_SureRule:
SureUserId SureRuleId Update_Time Update_Host
9007 ……
[/Quote]

最终期望的结果贴一下

22,207

社区成员

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

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