1. mysql 类似oracle中的 decode 函数
IF(COALESCE(table.column,values),'v1','v2') case 示例:
- select id,name,if(coalesce(s=1),'男','女') s from string_test;
-
-
select id,name,if(s=1,'男','女') s from string_test;
-
-
select id,name,(case when s=0 then '男' when s=1 then '女' else '未知' end) from string_test;
-
-
select id,name,(case s when 0 then '男' when 1 then '女' else '未知' end) from string_test;
|
2. 插入数据参数调优- set global innodb_flush_log_at_trx_commit=2;
-
set global sync_binlog=0;
3. limit
- SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
-
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
-
-
-
SELECT * FROM table LIMIT 2 OFFSET 1;
-
比如这个SQL ,limit后面跟的是2条数据,offset后面是从第1条开始读取。
-
SELECT * FROM table LIMIT 2,1;
-
而这个SQL,limit后面是从第2条开始读,读取1条信息。
-
-
-
查询时间最新目的几条记录的SQL语句:
-
SELECT * FROM table order by time desc LIMIT n;
4. sql_calc_found_rows 用法
- select sql_calc_found_rows count(id) as total,email,message from log where email like '%163.com' group by message;
-
+-------+-----------------------+-----------------------+
-
| total | email | message |
-
+-------+-----------------------+-----------------------+
-
| 1 | kaka@163.com | adding employee kaka |
-
| 1 | www.wjlcn.com@163.com | adding employee wjlcn |
-
| 2 | kaka@163.com | password change |
-
| 1 | kaka@163.com | removing employee |
-
+-------+-----------------------+-----------------------+
- SELECT FOUND_ROWS();
阅读(1986) | 评论(0) | 转发(0) |