mysql使用正则表达式查询

楼主
mysql使用正则表达式查询
[P]属性名 regexp ‘匹配方式’[/P][P]正则表达式的模式字符[/P][P]^ 匹配字符开始的部分[/P][P]eg1:  从info表name字段中查询以L开头的记录[/P][P]       select * from info where name regexp '^L';[/P][P]eg2:  从info表name字段中查询以aaa开头的记录[/P][P]       select * from info where name regexp '^aaa';[/P][P]$ 匹配字符结束的部分[/P][P]eg1:   从info表name字段中查询以c结尾的记录[/P][P]       select * from info where name regexp 'c$';[/P][P]eg2:   从info表name字段中查询以aaa结尾的记录[/P][P]       select * from info where name regexp 'aaa$';[/P][P].  匹配字符串中的任意一个字符,包括回车和换行[/P][P]eg1:   从info表name字段中查询以L开头y结尾中间有两个任意字符的记录[/P][P]      select * from info where name regexp '^L..y$';[/P][P][字符集合]匹配字符集合中的任意字符[/P][P]eg1:   从info表name字段中查询包含c、e、o三个字母中任意一个的记录[/P][P]      select * from info where name regexp '[ceo]';[/P][P]eg2:   从info表name字段中查询包含数字的记录[/P][P]       select * from info where name regexp '[0-9]';[/P][P]eg3:   从info表name字段中查询包含数字或a、b、c三个字母中任意一个的记录[/P][P]       select * from info where name regexp '[0-9a-c]';[/P][P][^字符集合]匹配除了字符集合外的任意字符[/P][P]eg1:   从info表name字段中查询包含a-w字母和数字以外字符的记录[/P][P]      select * from info where name regexp '[^a-w0-9]';[/P][P]s1|s2|s3 匹配s1s2s3中的任意一个[/P][P]eg1:   从info表name字段中查询包含'ic'的记录[/P][P]      select * from info where name regexp 'ic';[/P][P]eg2:   从info表name字段中查询包含ic、uc、ab三个字符串中任意一个的记录[/P][P]      select * from info where name regexp 'ic|uc|ab';[/P][P]*  代表多个该字符前的字符,包括0个或1个[/P][P]eg1:   从info表name字段中查询c之前出现过a的记录[/P][P]      select * from info where name regexp 'a*c';[/P][P]+  代表多个该字符前的字符,包括1个[/P][P]eg1:   从info表name字段中查询c之前出现过a的记录[/P][P]      select * from info where name regexp 'a+c';(注意比较结果!)[/P][P]字符串{N} 字符串出现N次[/P][P]eg1:   从info表name字段中查询出现过a3次的记录[/P][P]      select * from info where name regexp 'a{3}';[/P][P]字符串{M,N}字符串最少出现M次,最多出现N次[/P][P]eg1:   从info表name字段中查询ab出现最少1次最多3次的记录[/P][P]      select * from info where name regexp 'ab{1,3}';[/P][P]     [/P][P]MYSQL中自带通配符(LIKE关键词)[/P][P]%可以表示任意长度的字符(包括0)[/P][P]-可以表示单个字符[/P][P]
重名帅选   select `name` ,count(`name`) as aa  from 学籍表   group by `name` having   count(`name`)>=2;
select gradeid, `name` ,count(`name`) as aa  from ttt where gradeid=1 group by `name` having   count(`name`)>=2  ;
    select * from ttt where `name` regexp '[0-9]';[/P][P]mysql 两个字段 后半部分部分相同 如何实现连表查询
 news表 id为mediumint(8)
id    title
3    文章标题1
4    文章标题2
5    文章标题3[/P][P]hits表 文章点击次数表有两个字段:
hitsid char(30) 型
weekviews int(10) 型
比如下面的字段值 c-1-3中的3与news表中的文章id一致:
hits id   weekviews
c-1-3      56
c-1-4      12
c-1-5      23[/P][P] [/P][P]请问,如何联表查询 对应文章的点击次数.并按点击降序. 关键是id字段类型不同,字段值也不完全相同.下面是我的写法.不对
SELECT * FROM `news` a, `hits` p  WHERE   CAST(p.hitsid as SIGNED)-CAST("c-1-" as SIGNED) =a.id  ORDER BY a.id DESC[/P][P] [/P][P]MySQL数据库批量去掉某一个字段值中前几位字符
比如,在表1中,字段 csysid 有些值为 OM, 对应的CID 的值应该为数字,01,02,03... 但是不知道为什么 所有的数字前面都加了0M这两个 字母,变成om01、om02、om03......现在要把这两个字母去掉,该如何写语句。如何单纯针对上边这个表的话,因为前两个字母一样,所以可以批量把前两个字母去掉就可以了。
update 表1 set CID=replace(CID,'om','')[/P][P]如果有些表前两个字符并不一样,那么就需要使用如下语句了:
update 表1 set CID=substring(CID,3,len(CID))[/P][P]这是去掉前两个字符的,如果需要去掉3位或更多字符那么修改下相应的数字就可以了。[/P][P]最佳答案[/P][P]SELECT *
FROM `news` a, `hits` p
WHERE CONCAT(  'c-1-', a.id ) = p.hitsid
ORDER BY p.weekviews DESC[/P]
1楼
[P]还是挺有用了[/P]
2楼
这些知识很复杂,我看不明白
3楼
今天第一帖。。。。。

电脑版 Page created in 0.0703 seconds with 4 queries.