求教这个存储过程怎么写

倾城之恋 2011-08-28 04:18:37
userbaseinfo(用户信息表) 有字段
自增,bit, varchar(15)
ID, isbuy, price
123, 1, 2000
124, 0, 3000
125, 0, 8000
次表condition(买卖条件表) 有字段
自增,bit, varchar(15)
ID, isbuy, pricerange,userid(对应主表id)
2, 0, 2000~3000, 123
3, 1, 3000~5000, 125
如何通过一个变量@userid(如123),求出主表所有isbuy<>0(0为买方,1为卖方),且价格区间在2000-3000的记录
我的SQL比较菜,老是弄不出这个存储过程,特来求教。非常感谢。。
...全文
86 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
倾城之恋 2011-08-28
  • 打赏
  • 举报
回复
我提供的表只是为了说明,实际上的应用比这个要复杂一些,因为有其它有应用所以有必要在两表设置同一字段,不过在这个例子中倒是可以设置反向的,谢谢您提供的例子,我自己再调试一下
-晴天 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 fulans1 的回复:]
经测试,上面的语句找出的不是想要的结果,思路好象有点不对
比如如果condition的ISBUY=0,求出来的应该所有userbaseinfo的ISBUY=1,但现在是0,1都找出
[/Quote]

既然condition表里设置了条件,而且你又是要从这个表中查询得到 isbuy 的值来限制对 userbaseinfo 的查询,那为什么一定要 所有userbaseinfo的ISBUY=1 呢?
如果是反向的,那把等于改成不等于就行了.
倾城之恋 2011-08-28
  • 打赏
  • 举报
回复
经测试,上面的语句找出的不是想要的结果,思路好象有点不对
比如如果condition的ISBUY=0,求出来的应该所有userbaseinfo的ISBUY=1,但现在是0,1都找出
-晴天 2011-08-28
  • 打赏
  • 举报
回复
还差一个:
declare @userid int
set @userid=123

select a.*
from userbaseinfo a inner join condition b on a.id=b.userid and a.isbuy=b.isbuy and
a.price between
convert(int,left(b.pricerange,charindex('~',b.pricerange)-1)) and
convert(int,right(b.pricerange,len(b.pricerange)-charindex('~',b.pricerange)))
where a.id=@userid
-晴天 2011-08-28
  • 打赏
  • 举报
回复
原来如此!
declare @userid int
set @userid=123

select a.*
from userbaseinfo a inner join condition b on a.id=b.userid and a.isbuy=b.isbuy and
a.price between
convert(int,left(b.pricerange,charindex('~',b.pricerange)-1)) and
convert(int,right(b.pricerange,len(b.pricerange)-charindex('~',b.pricerange)))
倾城之恋 2011-08-28
  • 打赏
  • 举报
回复
楼上的,变量只提供一个@userid
其它下面这几个变量是要能过@userid在CONDITION中求出来的,
set @isbuy=0
set @a=2000
set @b=3000
-晴天 2011-08-28
  • 打赏
  • 举报
回复
declare @userid int,@isbuy bit,@a int,@b int
set @userid=123
set @isbuy=0
set @a=2000
set @b=3000

select a.*
from userbaseinfo a inner join condition b on a.id=b.userid
where a.id=@userid and a.isbuy=@isbuy and
convert(int,left(b.pricerange,charindex('~',b.pricerange)-1))=@a and
convert(int,right(b.pricerange,len(b.pricerange)-charindex('~',b.pricerange)))=@b
-晴天 2011-08-28
  • 打赏
  • 举报
回复
create table condition(ID int,isbuy bit,pricerange varchar(20),userid int)
insert into condition select 2, 0, '2000~3000', 123
insert into condition select 3, 1, '3000~5000', 125
go
select convert(int,left(pricerange,charindex('~',pricerange)-1))a,convert(int,right(pricerange,len(pricerange)-charindex('~',pricerange)))b from condition
/*
a b
----------- -----------
2000 3000
3000 5000

(2 行受影响)

*/
go
drop table condition
倾城之恋 2011-08-28
  • 打赏
  • 举报
回复
有什么涵数可以类似C#中,split("~"),把2000~3000分成两个INT值
倾城之恋 2011-08-28
  • 打赏
  • 举报
回复
楼上的两位,isbuy <> 0这个0是个变量,要select * from condition where userid=@userid动态选出
另,'2000~3000'也是根据USERID选出的且需要转换成 where price>2000 and price<3000
-晴天 2011-08-28
  • 打赏
  • 举报
回复
select a.* 
from userbaseinfo a inner join condition b on a.id=b.userid
where a.id=@userid and b.pricerange='2000~3000'
--小F-- 2011-08-28
  • 打赏
  • 举报
回复
select
a.*
from
userbaseinfo a,condition b
where
a.id=b.userid
and
b.pricerange='2000~3000'
and
a. isbuy <> 0
and
b.userid=@userid
--小F-- 2011-08-28
  • 打赏
  • 举报
回复
select
a.*
from
userbaseinfo a,condition b
where
a.id=b.userid
and
b.pricerange='2000~3000'
and
b.userid=@userid
chuanzhang5687 2011-08-28
  • 打赏
  • 举报
回复
create proc procName
@userid int
as
begin
select a.* from 主表 a, 次表 b
where a.id = b.id and a. isbuy <> 0 and
b.pricerange = '2000-3000' and b.userid = @userid
end
chuanzhang5687 2011-08-28
  • 打赏
  • 举报
回复
create proc procName
@id int
as
begin
select a.* from 主表 a, 次表 b
where a.id = b.id and a. isbuy <> 0 and
b.pricerange = '2000-3000' and a.id = @id
end
chuanzhang5687 2011-08-28
  • 打赏
  • 举报
回复
select a.* from 主表 a, 次表 b  where a.id = b.id and a. isbuy <> 0 and and b.pricerange = '2000-3000'

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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