多条件问题

lincai 2007-06-28 04:54:55
ID 字段1 字段2 字段3
1 a h 0
2 b i 5
3 b p 6
4 c f 1

现在要根据字段1 字段2 字段3 的值来算钱

有时 字段1的值等于 a 时,那么钱=50
有时 字段1的值等于 b ,同时 字段2 = i 时 钱=100
字段2的值等于 b ,同时 字段2 = p 时 钱 = 200
....

请问怎样设置个表来算价钱,又如何算出这些价钱,百分感谢!
...全文
195 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lincai 2007-06-29
  • 打赏
  • 举报
回复
up
yrwx001 2007-06-28
  • 打赏
  • 举报
回复
那你在上面表增加個 money 字段
客戶配置時
你檢查 if exists( select 1 from table where field1 = @客戶配置值1 and field2= @客戶配置值2 and field3= @客戶配置值3)
update table set money = @客戶配置money值 where field1 = @客戶配置值1 and field2= @客戶配置值2 and field3= @客戶配置值3
else
insert table values (@客戶配置值s)
然後你查哪個 就select 哪個
不就好啦
lincai 2007-06-28
  • 打赏
  • 举报
回复
可以给代码看看吗?
肥胖的柠檬 2007-06-28
  • 打赏
  • 举报
回复
这如你所说的,性能相当差..我这也有这样的设计,不过是保存是代码,也可以说是前台的公式~
lincai 2007-06-28
  • 打赏
  • 举报
回复
这样能实现,但是性能相当差,如果配置表有N条记录就得循环N次,再加上tbl这张表记录一年增加10W。
lincai 2007-06-28
  • 打赏
  • 举报
回复
我现在的思路就是用一张表来保存配置条件的数据,然后读出SQL条件合并查询SQL字符串来执行,比如SqlCondition 字段值:"字段1 = b and 字段2 = i" Money字段值: 100
那么合并就是 "select * from tbl where " + SqlCondition + "ID=x" ;循环读取配置表里的记录,读到有记录时就能取到money了。
lincai 2007-06-28
  • 打赏
  • 举报
回复
也就是说可以让用户来配置满足一定的条件时给多少钱。
bill024 2007-06-28
  • 打赏
  • 举报
回复
没看到回复
bill024 2007-06-28
  • 打赏
  • 举报
回复
select ID,字段1,字段2,字段3,钱=case when 字段1='a' then 50
when 字段1='b' and 字段2='i' then 100
when 字段1='b' and 字段2='p' then 200 end
from test
lincai 2007-06-28
  • 打赏
  • 举报
回复
这些值不是固定的,在程序运行时可以让用户配置
yrwx001 2007-06-28
  • 打赏
  • 举报
回复
select 价钱 = (case 字段1 when a then 50
when b then
(case 字段2 when i then 100
when p then 200 end)....end) from table
bill024 2007-06-28
  • 打赏
  • 举报
回复
select 钱=case when 字段1='a' then 50
when 字段1='a' and 字段2='i' then 100
when 字段1='b' and 字段2='p' then 200 end
from 表

22,209

社区成员

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

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