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.字符串拼 ...
随机推荐
- Java类文件结构及javac的ClassReader类解读
首先来看一下ClassFile,类注释如下: A JVM class file. Generic Java classfiles have one additional attribute for c ...
- guava学习:guava集合类型-Bimap
学习guava让我惊喜的第二个接口就是:Bimap BiMap是一种特殊的映射其保持映射,同时确保没有重复的值是存在于该映射和一个值可以安全地用于获取键背面的倒数映射. 最近开发过程中,经常会有这种根 ...
- svn 被锁,清理恢复过程
svn 被锁,清理恢复过程 http://stackoverflow.com/questions/18000363/tortoisesvn-wont-allow-me-to-add-any-files ...
- Python基础 - 总则
学习Python的笔记,有基础语法,有注意点.仅此而已. 目录: ------------------------------------------- Python基础(1) - 初识Python ...
- 决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.的解决方案
1.异常信息: C:\Python36\python36.exe "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6- ...
- 如何封装一个Cookie库
由Cookie详解我们已经了解到了Cookie是为了实现有状态的基于HTTP协议的应用而存在的.一个Cookie的由以下几个部分组成: //设置cookie的格式和Set-Cookie头中使用的格式一 ...
- 【angular5项目积累总结】优秀组件以及应用实例
1.手机端 图片预览组件 组件:sideshow 效果图:(预览图全屏 且可以左右移动) code: <div class="row ui-app-s ...
- WCF WCF的宿主
一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...
- Docker学习之Docker镜像基本使用
Docker学习之Docker镜像基本使用 获取镜像 命令格式:docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签] 例如: docker pull ...
- Spring FactoryBean用法
最近在看spring ioc源码,看到FactoryBean这个内容.这个和BeanFactory的区别 1. BeanFactory: 生成bean的工厂,是一个接口,定义了很多方法 2. Fact ...