查询条件:
$sort_id = '11,21,23';
查询表:
id sort
1 11,22
2 21,23
3 50,60
4 23,60
查询结果:
id sort
1 11,22
2 21,23
4 23,60
ID 3 ,没有任何匹配,所以未被列出。
请问这样的MYSQL语句应该怎么写?
$sort_id = '11,21,23';
查询表:
id sort
1 11,22
2 21,23
3 50,60
4 23,60
查询结果:
id sort
1 11,22
2 21,23
4 23,60
ID 3 ,没有任何匹配,所以未被列出。
请问这样的MYSQL语句应该怎么写?
where sort REGEXP concat('[' & replace('11,21,23',','|') & ']');http://dev.mysql.com/doc/refman/5.1/zh/functions.html#string-comparison-functions
select * from yourTable where sort REGEXP
concat('[', replace('11,21,23',','|'), ']');
concat('[', replace('11,21,23',',','|'), ']');
select * from aa where sort REGEXP
concat('[', replace('11,21,23',',','|'), ']'); 目的是要查出sort字段(字符串类型)中所有包含'11,21,23',可查出来的sort数据中还有其它的数据而不是我要的'11,21,23'另外我的数据库都是utf8格式的
1,11,19
1,8,26
1,8,9
1,11,20
1,2,3,0
1,8,26
1,8,26
1,8,0
1,2,3,29,30
1,8,26
1,2,7
1,12,25
1,2,0
1,8,0
1,2,3,29,30
1,2,3,29,0
1,2,3,29,30 SQL这样写:select * from tab where sort REGEXP concat('[', replace('8,11,',',','|'), ']'); 目的是想查出字符串中包含8,11的所有数据,可结果如下:
1,11,19
1,8,26
1,8,9
1,11,20
1,2,3,0
1,8,26
1,8,26
1,8,0
1,2,3,29,30
1,8,26
1,2,7
1,12,25
1,2,0
1,8,0
1,2,3,29,30
1,2,3,29,0
1,2,3,29,30
后面的3条和上面几条不是我要的数据啊
concat(',',aa,',') where sort REGEXP
concat('[', replace('11,21,23',',','|'), ']');
where sort REGEXP concat('[', replace('8,11',',','|'), ']'); 你多了个逗号.
replace('8,11,',',','|')
-->>
replace('8,11',',','|')
where concat(',',sort,',') REGEXP concat(replace('8,11',',','|'));
where sort REGEXP concat(replace('8,11',',','|'));
结果连 1,8,26 这个也查出来了
把'2,29'改成'3,29'就可以返回0