请问下面这个查询语句怎么写

小菲1215 2007-06-11 09:00:55
表结构
序号  姓名  费用
1 小张  1.2
2    小李 1.3
3   小王   1.4
4    小邓   1.5
我要怎么判断小李的费用是大于小王的费还是小于他的费用
...全文
166 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
FlyFish1285 2007-06-11
  • 打赏
  • 举报
回复
声明一个变量
declare @bool int --如果大于,则置1,否则置0
--读取数据库中的小李的费用
declare @costl float
select @costl = 费用 from biao where 姓名 = '小李'
declare @costw float
select @costw = 费用 from biao where 姓名 = '小王'
if @costl >@costw
set @bool = 1
else
set @bool = 0
这有一定的问题,在计算机中两个浮点数比较大小是不合适的!呵呵..不过可以参考一下
nobody@noone 2007-06-11
  • 打赏
  • 举报
回复

select * from tablename where 费用 >(select 费用 from tablename where name = '小李') order by id dsec
wgsasd311 2007-06-11
  • 打赏
  • 举报
回复
表结构
序号  姓名  费用
1 小张  1.2
2    小李 1.3
3   小王   1.4
4    小邓   1.5
5    小王   1.3
6    小李   1.1
我要查询小王大于小李的费用的序列号,
==>
姓名可以重复的话,楼主的要求(查询小王大于小李的费用的序列号)是不合理的,因为当,小李也有好几个出现时,你到底是怎样比呢,是拿小李最小费用的和小王作比较,还是最大的?
肥胖的柠檬 2007-06-11
  • 打赏
  • 举报
回复
select * from #test a where exists(select 1 from #test where a.费用>费用 and a.姓名='小王' and 姓名='小李')
--这个是大于小李最少的
paoluo 2007-06-11
  • 打赏
  • 举报
回复
try

Select * From TableName A
Where Exists(Select 姓名 From TableName Where 姓名 = N'小李' And 费用 < A.费用)
And 姓名 = N'小王'
小菲1215 2007-06-11
  • 打赏
  • 举报
回复
这样来,刚刚忘了说
表结构
序号  姓名  费用
1 小张  1.2
2    小李 1.3
3   小王   1.4
4    小邓   1.5
5    小王   1.3
6    小李   1.1
我要查询小王大于小李的费用的序列号,
bill024 2007-06-11
  • 打赏
  • 举报
回复
前提要保证姓名是唯一的
bill024 2007-06-11
  • 打赏
  • 举报
回复
create table test(序号 int,姓名 varchar(20),费用 decimal(5,1))
insert test select 1,'小张',1.2
union all select 2,'小李',1.3
union all select 3,'小王',1.4
union all select 4,'小邓',1.5

select distinct case when (select 费用 from test where 姓名='小李')-(select 费用 from test where 姓名='小王')>0 then
'大于小王'
when (select 费用 from test where 姓名='小李')-(select 费用 from test where 姓名='小王')<0 then
'小于小王'
when (select 费用 from test where 姓名='小李')-(select 费用 from test where 姓名='小王')=0 then
'等于小王' end
from test

drop table test

34,590

社区成员

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

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