MYSQL 测试常用语句使用技巧
select UNIX_TIMESTAMP(NOW()); -- 将时间转换为时间戳 1518428395
select FROM_UNIXTIME(1518428166); -- 将时间戳转换为时间 2018-02-12 17:36:06
select FROM_UNIXTIME(1518428166,'%Y-%m-%d') -- 2018-02-12
SELECT STR_TO_DATE(NOW(),'%Y-%m-%d'); -- 2018-02-12 java代码调用会出错
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %k:%i:%s'); -- 2018-02-12 17:44:33
select date_format(DATE_SUB(NOW(),INTERVAL +1 DAY), '%Y%m%d') -- 20180211 +为减去多少天 -为加多少天
SELECT DATE_SUB(CURDATE(),INTERVAL -1 DAY); -- 2018-02-13
随机数
set @min=1;
set @max =3;
select FLOOR(RAND() * @max-@min+1)+@min; -- 返回1-3之间的随机数 select FLOOR(RAND() * 56); -- 产生0-56的随机整数数
SELECT FORMAT(rand()*55,2); -- 0-55随机数,保留2位小数
小数处理
SELECT FORMAT(22.6655,2); -- 四舍五入 保留2位小数
SELECT truncate(1.228,2); -- 只舍不入 保留2位小数
UNION和 UNION ALL区别
select 'a1','a2' FROM DUAL UNION -- 会合并成1条数据 结果1
select 'a1','a2' from DUAL;select 'a1','a2' FROM DUAL UNION ALL -- 不会合并 还是2条数据 结果2
select 'a1','a2' from DUAL;

其他使用技巧
select 53+IFNULL(NULL,0);-- IFNULL 返回53
-- case WHEN
select uid, (CASE ISNULL(leader_uid) WHEN TRUE THEN 0 ELSE leader_uid END ) leader_uid ,channel_id from ( select uid,b.leader_uid,channel_id from t_mer a LEFT JOIN t_mer_group b on a.uid = b.member_uid) a
-- IFNULL(expr1,expr2)
select uid, IFNULL(leader_uid,0) ,channel_id from ( select uid,b.leader_uid,channel_id from t_mer a LEFT JOIN t_mer_group b on a.uid = b.member_uid) a
UPDATE
update t_user_game_binding t set t.member_uid = (select uid from t_mer where channel_id = t.chl_id limit 1);
-- 比上面那个高效
UPDATE t_user_game_binding LEFT JOIN t_mer ON t_user_game_binding.chl_id = t_mer.channel_id SET member_uid = t_mer.uid
Insert
-- insert包含join
INSERT INTO t_user_game_binding_1 (user_id,outter_game_id,chl_id,member_uid)
SELECT a.user_id,a.outter_game_id,a.chl_id,b.uid FROM t_user_game_binding a LEFT JOIN t_mer b on a.chl_id=b.channel_id; INSERT ignore t_user_game_binding_1…… -- 忽略错误
-- 根据条件insert insert into ... select ..from dual where EXISTS ()
INSERT INTO `test` (`t1`) SELECT 'x' from dual where not EXISTS (select * from test);
-- 避免插入重复的数据详情点击 https://www.cnblogs.com/duanxiaojun/p/6855680.html?utm_source=itdadao&utm_medium=referral
其他使用技巧
http://www.jb51.net/article/42229.htm 随机取数据
-- 随机排序
SELECT * FROM t_game ORDER BY RAND() LIMIT 1; select CONCAT('a','','') -- 返回 a1112 select * from t_user_register_chl t where t.user_register_chl =1 and user_register_account REGEXP '^[a-z].*$' -- 正则表达式匹配
-- 正数 负数转换
select -1*55,ABS(-11);
-- 向前补位 0001
select LPAD('',3,0);
-- 向后补位 1000
select RPAD('',3,0);
修改DDL
alter table tableName modify column redeem_code VARCHAR(500) NOT NULL DEFAULT '' COMMENT '兑换码'; -- 修改字段长度
alter table t_user_checked_record ADD id BIGINT auto_increment primary key; -- 增加自增列
mysql 性能分析 EXPLAIN
EXPLAIN select * from user_base_info t where uid='';
-- 设置cache大小
SET GLOBAL max_binlog_cache_size =1009715200;
SET GLOBAL binlog_cache_size =1009715200;
-- select into 一定要初始化变量
SET @id = NULL;
SET @n = NULL;
select game_id,outter_game_id INTO @id,@n from t_game where game_id%5=0 limit 1;
select @id,@n;
关于教程:http://www.w3school.com.cn/sql/index.asp
MYSQL 测试常用语句使用技巧的更多相关文章
- DBA必备:MySQL数据库常用操作和技巧
DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...
- mysql的常用语句
Mysql的常用语句 -- 创建表 create table tableName( id int primary key, name varchar(20) ) -- 查询 select * from ...
- mysql数据库常用语句2
关于mysql常用语句的整理,上一篇涉及到ddl.dml以及一些简单的查询语句. 1:mysql分页查询 select * from table_name limit 5,10; 从下标为5元素查 ...
- mysql数据库常用语句
关于mysql数据库常用命令的整理: 一:对于数据库的操作 show databases;显示当前用户下所有的数据库名称 use database_name;进入当前数据库 create databa ...
- MySQL:常用语句
ylbtech-MySQL:常用语句 1.返回顶部 1. -- ---------------------------- -- Table structure for st_student -- -- ...
- MySQL数据库常用操作和技巧
MySQL数据库可以说是DBA们最常见和常用的数据库之一,MySQL的广泛应用,也使更多的人加入到学习它的行列之中.下面是老MySQL DBA总结的MySQL数据库最常见和最常使用的一些经验和技巧,分 ...
- mysql sql常用语句大全
SQL执行一次INSERT INTO查询,插入多行记录 insert into test.person(number,name,birthday) values(5,'cxx5',now()),(6, ...
- mysql数据库常用语句3
一:查询指定数据库中所有的表名 数据库名:test select table_name from information_schema.tables where table_schema='test' ...
- MySql中常用语句
1.查询语句: SELECT 查询字段 FROM 表名 WHERE 条件 查询字段可以使用 通配符* 字段名 别名(把长的名字命名一个别名,比较短的) 通配符:SELECT * FROM ' ...
随机推荐
- Apache Accumulo
Apache Accumulo 是一个可靠的.可伸缩的.高性能的排序分布式的 Key-Value 存储解决方案, 基于单元访问控制以及可定制的服务器端处理.Accumulo使用 Google BigT ...
- (四)、Fiddler打断点
一.打断点是Fiddler一个比较好用的功能,它可以做一些手工操作很难做的事情. 那为什么要打断点? 看下图,Fiddler打开后,Client(客户端)发送的请求会先经过Fiddler,然后Fidd ...
- Python2 和 Python3 的区别(待完善)
1.宏观上 python2 :源码不标准,混乱,重复代码太多 python3 :统一 标准,去除重复代码. 2. print python2 :括号可有可无 print(a) 或 print ap ...
- 安装sphinx报错(undefined reference to `libiconv_open' 、undefined reference to `libiconv'、undefined reference to `libiconv_close'、make[1]: *** No rule to make target `all'. Stop. 、make: *** [all-recursive
(为知笔记copy过来格式有变,希望对遇到此问题的童鞋有帮助) 具体错误: Thank you for choosing Sphinx! [root@vm-vagrant csft-4.1]# mak ...
- 性能优化之mysql索引优化
sql及索引优化 如何通过慢查询日志发现有问题的sql? 查询次数多且每次查询占用时间长的sql通常为pt-query-digest分析的前几个查询 IO大的sql注意pt-query-digest分 ...
- springboot+idea 热部署
1 配置pom.xml <!--spring-boot-devtools 热部署--> <dependency> <groupId>org.springframew ...
- mac下的安装神奇 brew --例子 安装 mysql
da打开终端 输入bre 输入 bre search mysql (查找mysql版本) 输入 bre install mysql@5.6(选择mysql版本安装)
- line 1: syntax error: unexpected word (expecting ")")
编译出来的程序在arm平台上运行时,出现下面的错误. / # wpa_supplicant -B -c/etc/wpa_wpa2.conf -iwlan0 /bin/wpa_supplicant: ...
- OD 实验(一) - 修改程序标题
需要修改的程序 把 I love fishc.com 修改为 hello world sch01ar 用 OD 打开程序 在程序入口处开始一直按 F8 运行程序,看看在哪里弹出对话框 运行到该地址的时 ...
- 给深度学习入门者的Python快速教程
给深度学习入门者的Python快速教程 基础篇 numpy和Matplotlib篇 本篇部分代码的下载地址: https://github.com/frombeijingwithlove/dlcv_f ...
select 'a1','a2' FROM DUAL UNION ALL -- 不会合并 还是2条数据 结果2