需求:表中有两个字段 a b 其中 b的值为preStr_x(如preStr_width,preStr_height)
大概数据如下
a | b |
hello | preStr_hello |
hello | preStr_world |
hello | preStr_java |
hi | preStr_hi |
要求查询出a=x的记录,即上图中第2,4条记录
废话不多说,直接上sql
select s1.id FROM (
select t1.id 'id',t1.a,SUBSTRING(t1.[b],CHARINDEX('_',t1.b)+1,LEN(t1.b)) name1
FROM tableName t1
) s1
LEFT JOIN
(
select t1.id 'id',t1.a,SUBSTRING(t1.[b],CHARINDEX('_',t1.b)+1,LEN(t1.b)) name1
FROM tableName t1
) s2
on s1.id= s2.id
WHERE s1.a =s2.name1