-- 查询语句
select class from stu_info where sid=1000000102;
select * from stu_info t where t.age=88; -- t是表的别名,多表查询时比较方便
select * from atable a, btable b where a.aID = b.bID;
select * from stu_info t where t.age=99 or (t.age>20 and t.age <90);
select * from stu_info t where t.age between 10 and 99;
select * from stu_info t where t.hometown like '110%' ; -- %通配符,表示0到多个任意字符
select * from stu_info t where t.hometown like '_10%' ; -- _通配符,表示任意一个字符
select * from atable a where a.aID in (1,2);
select * from atable a where a.aID not in (1,2);
select * from atable a where a.aID in (select bID from btable);
select * from atable order by aID desc -- asc升序|desc降序,默认为asc
select name,count(*) as total from score_info group by name;
select * from stu_info limit 10; -- 查询前十条,起始位置默认为0,且limit只能放到sql语句的最后
select * from stu_info limit 101,110; -- 查询101-110条记录

-- 更新语句
update stu_info t set t.class='ceshi' where t.age=88;
select * from stu_info t where t.class='ceshi';

-- 插入语句
insert into atable (aID,aNAME) values (8,'test'); -- 基本语法
insert into atable values (6,'a20050111' ),( 7,'a20050112'); -- 不指定字段,则由左向右依次插入值
insert into atable select * from btable; -- 前提是表结构一样或是按字段插入
insert into atable select * from btable on duplicate key update anum=99;-- 如果存在主键冲突,则跳过主键执行update操作
select * from atable;
select * from btable;

-- 删除语句
test.rollback()
delete from atable where aID=8; -- 条件删除,如果不加条件,则全表数据删除,可回滚
truncate table atable; -- 删除全表数据,删除效率要高于delete,但是删除操作不可回滚
drop table atable; -- 直接删表

-- 修改表结构语句
desc score_info; -- 查询表结构
alter table score_info rename to score; -- 修改表名
alter table score_info add sex char(2); -- 增加字段
alter table score_info drop sex; -- 删除字段
alter table score_info modify sex varchar(2); -- 修改字段类型,modify只可以修改字段类型
alter table score_info change sex sosex char(2) -- 修改字段名称和字段类型

-- 联表查询
select * from a left join b on a.aID = b.bID; -- 左连接,查询出a表的全部记录,b表符合条件的记录
select * from a right join b on a.aID = b.bID; -- 右连接,查询出b表的全部记录,a表符合条件的记录
select * from a inner join b on a.aID = b.bID; -- 内连接或相等连接,取交集
select * from a,b where a.aID = b.bID; -- 等价于内连接
select * from a union select * from b; -- 查询a与b表的并集,结果去重
select * from a union all select * from b;
select * from b

-- 常用函数
select rand(); -- 该函数结果返回0-1之间的浮点型随机数,不可有参数
select round(3.45); -- 该函数原型round(x,y),四舍五入方式返回最接近于参数x的数,其值保留到小数点后面y位,没有y时默认为0
select round(3.45,1);
select truncate(5.56,1); -- 函数原型truncate(x,y),参数一个不能少,返回x值保留y位小数
select floor(9.88); -- floor(x)返回x的整数部分,采取直接截掉小数部分,同truncate
select floor(rand()*1000);
select curdate(); -- 返回当前日期,同current_date()
select CURTIME(); -- 返回当前时间,同current_time()
select now(); -- 返回当前日期时间
select now()+0; -- +0后会去掉时间分隔符,返回一个时间数字串
select date_format(now(),'%Y-%m-%d'); -- 格式化时间
select ltrim(' apple') as ltrim; -- 去掉左边空格
select rtrim(' apple ') as rtrim; -- 去右边空格
select trim(' apple ') as trim; -- 去首尾空格

sum(col);avg(col);count(col);min(col);max(col); -- 聚合函数常用到group by的select查询中

select length('apple'); -- 返回字符创长度
select concat(123,'apple',999); -- 拼接字符串
select concat_ws('-',2015,07,08); -- 拼接字符串,并以'-'作为分割符
select group_concat(anum) from a; -- 返回由一列值拼接组成的列表
select left('apple',2); -- 返回最左边的2个字符;
select right('apple',2); -- 返回最右边的2个字符
select lower('APPLE'); -- 返回小写
select upper('apple'); -- 返回大写
select reverse('apple'); -- 返回倒序字符串

Mysql常用语句与函数(待续)的更多相关文章

  1. MYSQL 常用语句与函数命令

    进图数据库mysql –u root –p 输入密码后进入 查看数据库: show databases; 进入数据库:use dvwa; 查看该数据库的表:show tables; 查操作: sele ...

  2. mysql常用语句和函数

    mysql语句如果长期不写,就会忘掉,所以要时常复习,温故而知新. 1.select length("中国人"),select char_length("中国人" ...

  3. MYSQL常用内置函数详解说明

    函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...

  4. MySQL 常用语句 (汇集)

    原文地址:MySql常用语句作者:wuyanle 一.mysql常用语句 创建,删除和最基本查询: 显示数据库  mysql->show databases; 创建数据库  mysql-> ...

  5. MySQL常用的系统函数

    MySQL常用的系统函数 2019年01月17日 17:49:14 pan_junbiao 阅读数 155    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csd ...

  6. MySQL 常用语句大全

    MySQL 常用语句大全 一.连接 MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例 1:连接到本机上的 MYSQL. 首先在打开 DOS 窗口,然后进入目录 my ...

  7. Mysql常用运算符与函数汇总

    Mysql常用运算符与函数汇总 本文给大家汇总介绍了mysql中的常用的运算符以及常用函数的用法及示例,非常的全面,有需要的小伙伴可以参考下 我们先把数据表建好 use test;create tab ...

  8. 0927—MySQL常用语句集合

    一.连接MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysql bin,再键入命令mysql ...

  9. oracle 和 mysql 常用语句对比汇总

    文章目录 一.数据库管理 1.1 用户管理 1.1.1 mysql用户.权限管理 1.1.2 oracle 用户.角色.权限管理 二.DQL 语句 2.1 基础查询 1.常量查询的区别: 2.字符串拼 ...

随机推荐

  1. js读取cookie信息

    1. 第一种方式读取cookie信息:用document.cookie.split(“; “)的方式把字符串分割成几个段,然后遍历整个数组 //javascript方法 function getCoo ...

  2. IE haslayout的理解与bug修复

    要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清楚的概念.大多 IE下的显示错误,就是源于 haslayout 什么是 haslayout ? ...

  3. [作业] Python入门基础--用户登陆

    让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定登陆 #__author:Mifen #date: 2018/11/28 import time #自定义本地用户名和密码 user_nam ...

  4. MyBatis Generator 详解(转)

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  5. Idea软件Vim插件问题

    人家说用webstorm是纯前端,用Idea是java+前端,好,那就用Idea,装上试试,全选所有插件安装,奇迹出现了,选中一行代码,backspace,删不了,我的天,好吧,复制粘贴的快捷键也不行 ...

  6. MySQL---2、安装与部署

    1.MySQL下载 MySQL版本的选择MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.MySQL Enterprise Edition 企业版本,需付费,可以 ...

  7. 字符串数组中含有json转换

    [{'a':'1','b':'2'},{'c':'3','d':'4'}]" 解决 import net.sf.json.JSONArray; import net.sf.json.JSON ...

  8. 图解源码之FutureTask篇(AQS应用)

    所以,FutureTask既可以由Executor来调度执行,也可以由调度线程调用FutureTask.run()直接执行. FutureTask是通过AQS的模板设计模式来实现阻塞get方法的. 从 ...

  9. Java内存区域与虚拟机类加载机制

    一.Java运行时数据区域 1.程序计数器 “线程私有”的内存,是一个较小的内存空间,它可以看做当前线程所执行的字节码的行号指示器.Java虚拟机规范中唯一一个没有OutOfMemoryError情况 ...

  10. 分ip统计网站访问次数

    package web.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; ...