如何选择与列中的值相对应的行,其中的条件如下所示。 [英] how to select the rows accroding to the value from column with where condition like this..

查看:58
本文介绍了如何选择与列中的值相对应的行,其中的条件如下所示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的表格

if i have a table like this

id  |  name  |  values 
1       abc     1,2,3,4,5,6
2       abc1     1,2,3,4,5,6,7,8,9
3       abc2     1,2,3,4,5,6,7,10,12,13,45
4       abc     1,2,3,4,5,6,15,16,45
5       abc3     1,2,3,4,5,6,7,8



i必须根据所有记录的值字段列中的@variable value ='7'选择那些记录。如何用逗号分割值

例如select * from table name其中'7'位于值


i have to select those record according to @variable value='7' in the values field columns of all records and how to split the values with commas
for example select * from table name where '7' lies in values

推荐答案

SELECT 
     *
FROM 
    YOURTABLE
WHERE
      values LIKE '%,' + @variable + ',%' --middle
      OR
      values LIKE @variable + ',%' --start
      OR
      values LIKE '%,' + @variable --end
      OR 
      values =  @variable --single 





或以上较短版本



Or shorter version of above

SELECT
     *
FROM
    YOURTABLE
WHERE  (',' + RTRIM(values) + ',') LIKE '%,' + @variable + ',%'



参考: http://stackoverflow.com/questions/5611715/where-value-in-column-containing-comma-delimited-values [ ^ ]


试试这个



Try this

select * from tablename where charindex(',7,',','+values+',')>0


从tablename中选择*,其中值为'%,7,%'
select * from tablename where values like '%,7,%'


这篇关于如何选择与列中的值相对应的行,其中的条件如下所示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆