Chinaunix首页 | 论坛 | 博客
  • 博客访问: 504140
  • 博文数量: 65
  • 博客积分: 2925
  • 博客等级: 上尉
  • 技术积分: 1306
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-01 10:56
个人简介

2013

文章分类

全部博文(65)

分类: Mysql/postgreSQL

2011-09-29 11:45:16

1. mysql 类似oracle中的 decode 函数
IF(COALESCE(table.column,values),'v1','v2')   case 示例:

  1. select id,name,if(coalesce(s=1),'男','女') s from string_test;

  2. select id,name,if(s=1,'男','女') s from string_test;

  3. select id,name,(case when s=0 then '男' when s=1 then '女' else '未知' end) from string_test;

  4. select id,name,(case s when 0 then '男' when 1 then '女' else '未知' end) from string_test;

2. 插入数据参数调优
  1. set global innodb_flush_log_at_trx_commit=2
  2. set global sync_binlog=0;

3. limit
  1. SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
  2. LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
  3.  
  4.  
  5. SELECT * FROM table LIMIT 2 OFFSET 1;
  6. 比如这个SQL ,limit后面跟的是2条数据,offset后面是从第1条开始读取。
  7. SELECT * FROM table LIMIT 2,1;
  8. 而这个SQL,limit后面是从第2条开始读,读取1条信息。
  9.  
  10.  
  11. 查询时间最新目的几条记录的SQL语句:
  12. SELECT * FROM table order by time desc LIMIT n;

4. sql_calc_found_rows 用法
  1. select sql_calc_found_rows count(id) as total,email,message from log where email like '%163.com' group by message;
  2. +-------+-----------------------+-----------------------+
  3. | total | email | message |
  4. +-------+-----------------------+-----------------------+
  5. | 1 | kaka@163.com | adding employee kaka |
  6. | 1 | www.wjlcn.com@163.com | adding employee wjlcn |
  7. | 2 | kaka@163.com | password change |
  8. | 1 | kaka@163.com | removing employee |
  9. +-------+-----------------------+-----------------------+
  10. SELECT FOUND_ROWS();
阅读(1986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~