Mysql常用语句与函数(待续)
-- 查询语句
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常用语句与函数(待续)的更多相关文章
- MYSQL 常用语句与函数命令
进图数据库mysql –u root –p 输入密码后进入 查看数据库: show databases; 进入数据库:use dvwa; 查看该数据库的表:show tables; 查操作: sele ...
- mysql常用语句和函数
mysql语句如果长期不写,就会忘掉,所以要时常复习,温故而知新. 1.select length("中国人"),select char_length("中国人" ...
- MYSQL常用内置函数详解说明
函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...
- MySQL 常用语句 (汇集)
原文地址:MySql常用语句作者:wuyanle 一.mysql常用语句 创建,删除和最基本查询: 显示数据库 mysql->show databases; 创建数据库 mysql-> ...
- MySQL常用的系统函数
MySQL常用的系统函数 2019年01月17日 17:49:14 pan_junbiao 阅读数 155 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csd ...
- MySQL 常用语句大全
MySQL 常用语句大全 一.连接 MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例 1:连接到本机上的 MYSQL. 首先在打开 DOS 窗口,然后进入目录 my ...
- Mysql常用运算符与函数汇总
Mysql常用运算符与函数汇总 本文给大家汇总介绍了mysql中的常用的运算符以及常用函数的用法及示例,非常的全面,有需要的小伙伴可以参考下 我们先把数据表建好 use test;create tab ...
- 0927—MySQL常用语句集合
一.连接MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysql bin,再键入命令mysql ...
- oracle 和 mysql 常用语句对比汇总
文章目录 一.数据库管理 1.1 用户管理 1.1.1 mysql用户.权限管理 1.1.2 oracle 用户.角色.权限管理 二.DQL 语句 2.1 基础查询 1.常量查询的区别: 2.字符串拼 ...
随机推荐
- split使用和特殊使用(包括截取第一个字符后的数据)
javaScript中关于split()的使用 1.一般使用对一个字符串使用split(),返回一个数组 例子: var testArr = "1,2,3,4,5": var ...
- 关于clear与清除浮动
今天看bootstrap突然看到了 .container:after { clear: both; } 好像对clear的用法有点模糊,于是于是又研究一下用法. 上面搜资料总会搜到张鑫旭老师的相关文章 ...
- leetcode2:线性表
/********************************************** Function:input two array and find the kth value the ...
- mongo小记
进mongo mongo 先添加admin表的账号密码 . use admin . db.createUser( { user: "admin", pwd: "admin ...
- Linux 的启动流程--转
http://cloudbbs.org/forum.php?mod=viewthread&tid=17814 半年前,我写了<计算机是如何启动的?>,探讨BIOS和主引导记录的作用 ...
- Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit c ...
- “System.OutOfMemoryException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
“System.OutOfMemoryException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 这个原因肯定不是因为程序内部的逻辑错误,或者别的什么情况. 想想,肯 ...
- JavaScript的原型链继承__propt__、prototype、constructor的理解、以及他们之间相互的关系。
回想自己已经工作了有一段时间了,但是自己对JavaScript的原型链.和继承的理解能力没有到位,最近他们彻底的整理并且复习了一遍. 本案例中部分文案来自网络和书籍,如有侵权请联系我,我只是把我的理解 ...
- Redis 【keys】 一句话说明
DEL----------------------------------删除给定的一个或多个key DUMP--------------------------------序列化给定key,并返回被 ...
- Vs2015+opencv2.4.10出现msvcp120d.dll丢失 opencv2410.props
今天vs2015配置好opencv编译运行会报错msvcp120d.dll丢失,只要把这个文件复制到你在第一步设置的Path环境变量路径里,和opencv的dll放在一起就行了.(初次配置完环境变量后 ...