数据更新 update

  1. update user set age = timestampdiff(year, brithdy, now());

查询数据 select

  • 查询全部
  1. select * from user;

  • 条件查询 where
  1. select * from user where id = 1;

  • 区间查询 between … and
  1. select * from user where id between 1 and 5;

  • 集合查询 in
  1. select * from user where id in(1,2,4,5);

  • 分组查询 count() max() min() avg() sum()
  1. //count()
  2. select gender, count(*) from user group by gender;
  3. //sum()
  4. select sum(age) from user;
  5. //avg()
  6. select avg(age) from user;
  • 分组条件查询 having,不能用where
  1. select gender, count(*) from user group by gender having gender = 'male';

  • 排序 order by
  1. select * from user order by age desc;

  • 分页查询 limit
  1. select * from user order by age desc limit 5;

关联查询

  • 内连接
  1. select u.id,u.name,t.id,t.name from user as u, test as t where u.id = t.id;
  2. //简化

  • inner join … on
  1. select * from user u join test t on u.id = t.id;
  2. //简化
  3. select * from user u join test t using(id);
  4. //条件
  5. select * from user u join test t using(id) where u.id = 1;

  • 左外连接 left join
  1. select * from user u left join test t using(id);

  • 右外连接 right join
  1. select * from user u right join test t using(id);

索引

索引应该创建在经常被查询的字段,或者常出现在结果集中的字段上

  • 创建索引
  1. //普通索引
  2. create index i_name on user (name);
  3. //唯一索引
  4. create unique index unique_gender on user(gender);

预处理

  • 防止 SQL 注入攻击
  • SQL 语句中的数据,只有在执行阶段再与字段进行绑定
  1. prepare stmt from 'select id,name from user where age > ? limit ?';
  2. set @age= 20, @num = 5;
  3. execute stmt using @age, @num;

更多相关文章

  1. 数据库CURD常用操作-select查询-预处理
  2. Sql Server之旅——第三站 解惑那些背了多年聚集索引的人
  3. Linq中带有迭代索引的Select扩展方法,为啥知道的人不多呢?
  4. 慎用ToLower和ToUpper,小心把你的系统给拖垮了
  5. Sql Server之旅——第十三站 深入的探讨锁机制
  6. 如何校验内存数据的一致性,DynamicExpresso 算是帮上大忙了
  7. 畅销3年的Python分布式爬虫课程 Scrapy打造搜索引擎
  8. 使用 Redis 实现一个轻量级的搜索引擎,牛逼啊!
  9. 你说的 Flink 和搜索引擎有什么关系

随机推荐

  1. SQL问题 ,要连接两个无直接关联表
  2. 1)如何用语句来查看一个表内是否建了索引
  3. Scripts:创建手工的SQL PROFILE的脚本,老
  4. 求一SQL语句(如何按某列的值分组且取出每
  5. 高手是怎样炼成的:精妙SQL语句介绍
  6. 手把手教你mysql(十五)游标变量流程控制
  7. CentOS 7 安装MyCli MySQL 客户端
  8. 处理SQL标准中的错误值
  9. SQL连接“实时错误'91'”
  10. sql merge的用法实例(学习日记)