SQL 小结

2017-01-22  本文已影响0人  欢乐相随

1、SQL查询某去重后的总数

select addrvalue from attack_t where addrtype='phone' group by addvalue;

2、在明确截取字段位置的情况下,可以使用left 和 substring 来实现替代通配符的功能

select distinct left(addrvalue,9) from attack_t where addrtype='phone' and(phoneArea ='' or phoneArea is null);

3、多条件查询

select * from 表名 case 条件 when '值1' then '替换值' 

when '值2' then '替换值2'

when '值3' then '替换值3'

update $table set phoneArea=case substring(addrvalue,1,7) when '$key' then '$value' end

4、替换新表后清除数据库中的缓存

flush table 表名

5、将字符串转为数字类型

cast(字段名 as SIGNED)

6、将SQL查询到的内容生成文件

select md5 from md5manage into oufile '/filename/aa.bcp'

7、截取 字符串

select substrinig (字段名,4,12) from host

8、instr

select instr(字段,字符串) from host

如果返回字符串在字段中出现的次数

9、from(select …)

select 字段名 from (select * from 表名 ) t //后面要加一个临时表名

10、多情况更新

update 表名 set region=(case adduserwhen '11' then 'aa' when '22' then 'bb' else adduser end)

11、使用通配符匹配汉字

select apply_unit from helpcase where not(apply_unit regexp "[u0394-uFFE5]") and left(case,2)='xc' group by apply_unit;

上一篇 下一篇

猜你喜欢

热点阅读