[英]Query MySQL LIKE with dot separator
我在mysql类型varchar中有字段,字段数据包含代码,代码就像树结构,点(。)作为父和子之间的分隔符。
示例1215
具有子1215.001
和1215.002
这是数据库中每一行的数据
ID | kode
1 | 1215
2 | 1215.001
3 | 1215.001.001
4 | 1215.002.001
5 | 1215.002
6 | 1215.002.001
如何获得2级代码?
它的意思是只有代码1215.001
和1215.002
已经尝试过这个查询
select * from `kegiatan` where `kode` LIKE '1215.%';
但它得到的所有代码都以1215
开头,例如: 1215.001.001
使用正则表达式。
select * from `kegiatan` where `kode` REGEXP '^1215\.[^\.]+$';
这符合一切:
That starts with ( "^" )
the string "1215.",
followed by at least one character that is a member of the set "NOT ." ("[^\.]+")
followed by the end of the string.("$")
你可以使用正则表达式;
select * from `kegiatan` where `kode` REGEXP '^1215.[0-9]+$';
哪个匹配以^1215
开头的项目,后跟一个句点.
,后跟一个或多个数值[0-9]+
,后跟字符串$
的结尾。
这将匹配1215.001
和1215.002
,但不是1215
或1215.001.001
希望它有所帮助,戴夫
select distinct substr(kode,1,8) from `kegiatan` where `kode` LIKE '1215.%';
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_substr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.