简单记录一下平时写sql时遇到的小技巧
查询
1.union自带排序与去重,如果不用去重用union ALL
2.or 会使 innodb索引失效,myisam没事
3.安全等于<=>结果包含NULL ,两者都为 NULL 时返回值为 1。
4.select distinct name from A 去重
5.as 起别名
条件语句
1.用if 与 case实现条件修改
update salary set sex = (case sex when ‘m’ then ‘f’ else ‘m’ end);
update salary set sex = if(sex=‘m’,‘f’,‘m’);
取余
mod(employee_id,2)
取字符串开头,结尾
LEFT(name,1)
RIGHT(name,2)
取ascii码
update salary set sex = char(ascii(‘m’) + ascii(‘f’) - ascii(sex));