我的已知条件是一个字符串(可编程拆分):'[a],[b],[c],[d]'Mysql数据表中的数据也是一个字符串为:
id str
1 '[a],[b],[c]' 三条都有,选中
2 '[a],[b],[d]' 三条都有,选中
3 '[a],[b],[c],[e]' [e]不存在,不能选中即输出:
id str
1 '[a],[b],[c]'
2 '[a],[b],[d]' 不用存储过程,怎样才能高效快速的得到这个结果呢?
请各位大虾不吝指教,谢谢
id str
1 '[a],[b],[c]' 三条都有,选中
2 '[a],[b],[d]' 三条都有,选中
3 '[a],[b],[c],[e]' [e]不存在,不能选中即输出:
id str
1 '[a],[b],[c]'
2 '[a],[b],[d]' 不用存储过程,怎样才能高效快速的得到这个结果呢?
请各位大虾不吝指教,谢谢
解决方案 »
- row 1 doesn't contain data for all columns
- 无法连接mysql
- mysql 连接查询 有blob字段 就查询不到结果 该怎么办?
- 为什么用pg_connect连接不上呢(不是 undefined fuction)
- 关于mysql的一个小的疑问
- 一个树型结构数据如何储存到关系型数据库中?
- 如何在linux下调用mysql c API
- MySQL的触发器是没有new这个关键字吗??
- mysql5.6.13 使用tuning-primer.sh脚本一直报错
- mysql如何指定指定排序展示
- 请教下用SQL能不能实现如下功能,谢谢
- sql语句选择*和name速度差多少?
[a],[b],[c]
为
[a]
[b]
[c]再判断是否都在[a],[b],[c],[d] 中
示例:
字符间隔为*,自行修改SELECT *,SUBSTRING_INDEX(
SUBSTRING_INDEX(a1.SUBJECT,'*',b.id),'*',-1)
FROM a a1 LEFT JOIN lsb b
ON (LENGTH(a1.SUBJECT)-LENGTH(REPLACE(a1.SUBJECT,'*','')))+1>=b.id;
+------+-----------------+
| id | str |
+------+-----------------+
| 1 | [a],[b],[c] |
| 2 | [a],[b],[d] |
| 3 | [a],[b],[c],[e] |
+------+-----------------+
3 rows in set (0.00 sec)mysql> select * from lzgame
-> where str regexp concat('^([[][',replace(replace(replace(@x,',',''),']',''),'[',''),'][]],?)*$');
+------+-------------+
| id | str |
+------+-------------+
| 1 | [a],[b],[c] |
| 2 | [a],[b],[d] |
+------+-------------+
2 rows in set (0.00 sec)mysql>
Query OK, 0 rows affected (0.00 sec)mysql>
from tb
where col1 regexp replace('[a],[b],[c],[d]',',','|')
replace函数里面的'[a],[b],[c],[d]'字符串即是你的已知字符串
id citys
6 [杭州],[上海],[北京] 对
7 [杭州],[西安],[北京] 错
8 [杭州],[上海] 对
9 [杭州],[上海],[南京] 错
10 [杭州],[上海],[北京],[南京] 错
比如我要找[杭州],[上海],[北京]是指该数据只能包含在这三个城市以内
因为要用到"与" ,不知道字符串正则该怎么写?请帮忙再看一下
我把表贴上来,请帮我再看一下,谢谢了!
create table test(
id bigint not null auto_increment,
citys varchar(200),
primary key (id)
);
insert into test(citys) value('[杭州],[上海],[北京]');
insert into test(citys) value('[杭州],[西安],[北京]');
insert into test(citys) value('[杭州],[上海]');
insert into test(citys) value('[杭州],[上海],[南京]');
insert into test(citys) value('[杭州],[上海],[北京],[南京]');要求输入条件:[杭州],[上海],[北京];
希望输出:
id | citys
1 | [杭州],[上海],[北京]
3 | [杭州],[上海]
也就是该数据只能包含在输入的[杭州],[上海],[北京]城市以内浪费了您的时间,深表歉意
纯用mysql的方案:select test.id,test.citys from (SELECT *, (LENGTH(test.citys)-LENGTH(REPLACE(test.citys,',','')))+1 len from test ) test join (
select id,sum(find) a from (
select test.*,a.len,find_in_set(a.a,'[杭州],[上海],[北京]')>0 find from test inner join (
SELECT test.*,SUBSTRING_INDEX(SUBSTRING_INDEX(test.citys,',',b.id),',',-1) a FROM (
SELECT *, (LENGTH(test.citys)-LENGTH(REPLACE(test.citys,',','')))+1 len from test
) test LEFT JOIN lsb b ON test.len>=b.id
) a on test.id=a.id
) a group by id
) i on test.id = i.id
where test.len=i.a;用程序拆分好输入数据的方案:select id ,citys,len
from (
SELECT *, (LENGTH(test.citys)-LENGTH(REPLACE(test.citys,',','')))+1 len
from test
) test
where len<=3 and
((find_in_set('[杭州]',citys)>0)+(find_in_set('[上海]',citys)>0)
+(find_in_set('[北京]',citys)>0)) =len;
谢谢"WWWWA"大大,但因为要生成全部字段的len字段,感觉会效率比较低,依然期待"ACMAIN_CHM"大大能指导一下正则的方案,谢谢!
感谢"WWWWA"大大和"ACMAIN_CHM"大大的指导,谢谢!