一个sql相减的问题 - MS-SQL Server / 疑难问题
一个字段是这样的 x 可以放多个值.. 传进来是字符串, ==> 12,13,14...20 如 select * from tableName where x in(12,13,14...20) 原始数据是这样..但是我想要得到 select * from tableName where x in(1,2,3...9)
也就是在原来的基础上减去11 ---?>>select * from tableName where x in(1,2,3...9)
没看明白 ,帮顶。
贴出表结构,数据
以及你要的结果
参考以下
SQL code:
create table #t(
id varchar(10))
declare @str varchar(300)
set @str='12,13,14,20'
declare @i int
declare @len int
set @i = 1
while @i < len(@str+',')
begin
insert #t select substring(@str+',',@i,charindex(',',@str+',',@i)-@i)
set @i = charindex(',',@str+',',@i)+1
end
select * from #t
/*
id
----------
12
13
14
20
(所影响的行数为 4 行)
*/
--drop table #t
create table a
(
id int identity(1,1) not null,
x int not null ----- 这个字段是保存1。..9 的数据 但是程序传过来的数据是 12,13,14...20 一个字符串
)
insert into a select 1 union all select 2 union all select 3 ,,,union all select 9
SQL code:
id varchar(10))
declare @str varchar(300)
set @str='12,13,14,20'
declare
相关问答:
可能因为工作的原因 接触数据库这块比较少,之前都是做程序这块,数据库这块都有专门的人来做 分工都很明细 所以对数据库这一块完全不了解。前段时间 去面试了几家公司 几乎都是在数据库这块挂掉的 连个简单的SQ ......
从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
<folder state="unchecked&qu ......
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......
通过NAME字段条件查询一个数据表,假设我有100个姓名,有以下两个方法,
方法1:
把100个Name 组成一个SQL语句,比如 Select * from tmp_table where Name='张三' or Name ='李四' Or ...Or Name='第一百个姓名'
......