查询,貌似有点难吧!

jwdream2008 2009-12-10 03:39:33
数据形式如下:
name PrintNum
张三 0
李四 1

实现结果的情况:
1:(未打印的记录)
name PrintNum
张三 0
2:(已打印的记录)
name PrintNum
李四 1
3:(全部记录)
name PrintNum
张三 0
李四 1
要求:
使用一个SQL语句实现上述三种要求的查询!
(根据传给SQL的参数是实现上面结果的三种情况之一)
...全文
141 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nianran520 2009-12-10
  • 打赏
  • 举报
回复

if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (name nvarchar(4),PrintNum int)
insert into [TB]
select N'张三',0 union all
select N'李四',1

declare @参数 int
select @参数 = 2
select * from tb
where PrintNum = case @参数 when 2 then PrintNum else @参数 end
----------------------
张三 0
李四 1
jwdream2008 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 happy_stone 的回复:]
引用 7 楼 jwdream2008 的回复:
传递的参数是查询的类型:0:表示要查询未打印的记录;1:表示查询已打印的记录;2:表示查询全部记录
select * from table1 where printnum=@参数???如何写??

SQL codeifobject_id('[TB]')isnotnulldroptable[TB]gocreatetable[TB] (namenvarchar(4),PrintNumint)insertinto[TB]select N'张三',0unionallselect N'李四',1--當@参数 為null時候查詢所有declare@参数intselect*from tbwhere printnum=isnull(@参数,printnum)--0:表示要查询未打印的记录set@参数=0select*from tbwhere printnum=isnull(@参数,printnum)--1:表示查询已打印的记录set@参数=1select*from tbwhere printnum=isnull(@参数,printnum)/*
(2 個資料列受到影響)
name PrintNum
---- -----------
张三 0
李四 1

(2 個資料列受到影響)

name PrintNum
---- -----------
张三 0

(1 個資料列受到影響)

name PrintNum
---- -----------
李四 1

(1 個資料列受到影響)*/
[/Quote]
非常感谢!不过printnum每打印一次会加1.所以数据库中printnum不一定都是1,可能是3等等?
这样你的
--1:表示查询已打印的记录
set @参数=1
select * from tb where printnum=isnull(@参数,printnum)
是不满足要求的!
nianran520 2009-12-10
  • 打赏
  • 举报
回复
select * from tb
where PrintNum = case @参数 when 2 then PrintNum else @参数 end
快乐_石头 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jwdream2008 的回复:]
传递的参数是查询的类型:0:表示要查询未打印的记录;1:表示查询已打印的记录;2:表示查询全部记录
select * from table1 where printnum=@参数???如何写??
[/Quote]
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (name nvarchar(4),PrintNum int)
insert into [TB]
select N'张三',0 union all
select N'李四',1
--當@参数 為null時候查詢所有
declare @参数 int
select * from tb where printnum=isnull(@参数,printnum)
--0:表示要查询未打印的记录
set @参数=0
select * from tb where printnum=isnull(@参数,printnum)
--1:表示查询已打印的记录
set @参数=1
select * from tb where printnum=isnull(@参数,printnum)
/*
(2 個資料列受到影響)
name PrintNum
---- -----------
张三 0
李四 1

(2 個資料列受到影響)

name PrintNum
---- -----------
张三 0

(1 個資料列受到影響)

name PrintNum
---- -----------
李四 1

(1 個資料列受到影響)


*/
jwdream2008 2009-12-10
  • 打赏
  • 举报
回复
传递的参数是查询的类型:0:表示要查询未打印的记录;1:表示查询已打印的记录;2:表示查询全部记录
select * from table1 where printnum=@参数???如何写??
Four 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
SQL codeselectsum(case PrintNumwhen0then1else0end)as 未打印的数据,sum(casewhen PrintNum>0then1else0end)as 已打印数据,count(1)as 全部数据from
tbwhere
name='参数'
[/Quote]

这个...不是刚才那个问题了...
快乐_石头 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
SQL codeselectsum(case PrintNumwhen0then1else0end)as 未打印的数据,sum(casewhen PrintNum>0then1else0end)as 已打印数据,count(1)as 全部数据from
tbwhere
name='参数'
[/Quote]
貌似不是这样吧
快乐_石头 2009-12-10
  • 打赏
  • 举报
回复
一條語句
標題也要顯示
1:(未打印的记录) ?
--小F-- 2009-12-10
  • 打赏
  • 举报
回复
select
sum(case PrintNum when 0 then 1 else 0 end) as 未打印的数据,
sum(case when PrintNum>0 then 1 else 0 end) as 已打印数据,
count(1) as 全部数据
from
tb
where
name='参数'
快乐_石头 2009-12-10
  • 打赏
  • 举报
回复
傳遞哪些參數?
jinjazzli 2009-12-10
  • 打赏
  • 举报
回复
传的是什么参数?

22,208

社区成员

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

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