日期:2014-05-18  浏览次数:20501 次

求一简单SQL语句.,.
字段 Tid 字符串类型,

 值: 1,2,3,5
  2,6,13,
  3,7

要求以逗号隔开把数据中带 3 的数据读取出来,第2条值是13不算
 即要得到第1条与第3条数据

数据库是Access的,。

------解决方案--------------------
SQL code

  select * from tab where Tid like '%3,%'

------解决方案--------------------
access啊、、语法好像不一样哦。。
------解决方案--------------------
sorry,1楼有bug,修正如下,
SQL code

create table lin
(Tid varchar(10))

insert into lin
select '1,2,3,5' union all
select '2,6,13' union all
select '3,7'


select * from lin 
where Tid like '%,3,%' or Tid like '3,%'

Tid
----------
1,2,3,5
3,7

(2 row(s) affected)

------解决方案--------------------
select * from tab where Tid like '%3,%'
--or
select * from tab where charindex ('3,',Tid)>0

自己试下!!
------解决方案--------------------
SQL code

--ACCESS中貌似是这样的
select * from tablename where Instr(','+Tid+',',',3,')>0

------解决方案--------------------
SQL code
if OBJECT_ID('tb')is not null
drop table tb
go
create table tb
(Tid varchar(10))

insert into tb
select '1,2,3,5' union all
select '2,6,13' union all
select '2,6,13' union all
select '2,6,13' union all
select '2,6,3' union all
select '3,7'


select * from tb 
where Tid like '%,3,%' or Tid like '3,%'or Tid like '%,3'


(6 行受影响)
Tid
----------
1,2,3,5
2,6,3
3,7

(3 行受影响)

------解决方案--------------------

以3结尾的你没考虑到。。

探讨
sorry,1楼有bug,修正如下,

SQL code

create table lin
(Tid varchar(10))

insert into lin
select '1,2,3,5' union all
select '2,6,13' union all
select '3,7'


select * from lin
where Tid like '%……

------解决方案--------------------
探讨
SQL code


declare @T table
(Tid VARCHAR(10))
insert into @T
select '1,2,3,5' union all
select '2,6,13' union all
select '3,7'

select * from @T WHERE CHARINDEX(',3,',','+Tid+',')>0
/*
Ti……

------解决方案--------------------
探讨
以3结尾的你没考虑到。。

------解决方案--------------------
select * from lin 
where Tid like '%,3,%' or Tid like '3,%' or Tid like '%,3'


------解决方案--------------------
SQL code
if OBJECT_ID('tb')is not null
drop table tb
go
create table tb
(Tid varchar(10))

insert into tb
select '1,2,3,5' union all
select '2,6,13' union all
select '2,6,13' union all
select '2,6,13' union all
select '2,6,3' union all
select '3,7'


select * from tb 
where Tid like '*,3,*' or Tid like '3,*'or Tid like '*,3'


(6 行受影响)
Tid
----------
1,2,3,5
2,6,3
3,7

(3 行受影响)