MySQL-SQL基础1
p.p1 { margin: 0; font: 11px Menlo; background-color: rgba(128, 128, 128, 0.5); min-height: 13px }
p.p2 { margin: 0; font: 11px Menlo; background-color: rgba(128, 128, 128, 0.5) }
span.s1 { font-variant-ligatures: no-common-ligatures }
#插入
mysql> insert into emp values('zzx1','2002-03-09','2009-04-03','2001',3,22);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values('ttx2','20023-04-10','2010-03-04','2002',4,23);
ERROR 1292 (22007): Incorrect date value: '20023-04-10' for column 'birth' at row 1
mysql> insert into emp values('ttx2','2023-04-10','2010-03-04','2002',4,23);
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 2002.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
#更新
mysql> update emp set sal=4000 where ename='ttx2';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from emp;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
mysql> select distinct deptno from emp;
+--------+
| deptno |
+--------+
| 1 |
| 3 |
| 4 |
+--------+
3 rows in set (0.01 sec)
#查询
mysql> select * from emp where deptno=1;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
+-------+------------+------------+---------+--------+------+
1 row in set (0.00 sec)
mysql> select * from emp where deptno=2;
Empty set (0.00 sec)
mysql> select * from emp where deptno=3;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
+-------+------------+------------+---------+--------+------+
1 row in set (0.00 sec)
mysql> select * from emp order by sal;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from emp order by deptno;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
#查询排序
mysql> select * from emp order by deptno,sal desc;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
#分页查询
mysql> select * from emp order by sal limit 3;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from emp order by sal limit 1,3;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
2 rows in set (0.00 sec)
#查询表总数
mysql> select count(1) from emp;
+----------+
| count(1) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
#针对emp表进行统计deptno的个数
mysql> select deptno,count(1) from emp group by deptno;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
+--------+----------+
3 rows in set (0.00 sec)
#对ename排序并计算
mysql> select ename,count(1) from emp group by ename;
+-------+----------+
| ename | count(1) |
+-------+----------+
| ttx2 | 1 |
| zzx1 | 2 |
+-------+----------+
2 rows in set (0.00 sec)
#对sal排序并计算
mysql> select sal,count(1) from emp group by sal;
+---------+----------+
| sal | count(1) |
+---------+----------+
| 2000.00 | 1 |
| 2001.00 | 1 |
| 4000.00 | 1 |
+---------+----------+
3 rows in set (0.00 sec)
#对age1排序并计算
mysql> select age1,count(1) from emp group by age1;
+------+----------+
| age1 | count(1) |
+------+----------+
| 21 | 1 |
| 22 | 1 |
| 23 | 1 |
+------+----------+
3 rows in set (0.01 sec)
#查看emp表结构
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(20) | YES | | NULL | |
| birth | date | YES | | NULL | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
| age1 | int(4) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
#对birth列排序并计算
mysql> select birth,count(1) from emp group by birth;
+------------+----------+
| birth | count(1) |
+------------+----------+
| 2000-01-01 | 1 |
| 2002-03-09 | 1 |
| 2023-04-10 | 1 |
+------------+----------+
3 rows in set (0.00 sec)
#对hirdate列排序并计算
mysql> select hirdate,count(1) from emp group by hirdate;
+------------+----------+
| hirdate | count(1) |
+------------+----------+
| 2000-01-01 | 1 |
| 2009-04-03 | 1 |
| 2010-03-04 | 1 |
+------------+----------+
3 rows in set (0.00 sec)
#统计部门人数同时统计总人数
mysql> select deptno,count(1) from emp group by deptno with rollup;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
| NULL | 3 |
+--------+----------+
4 rows in set (0.00 sec)
#统计ename同时统计总人数
mysql> select ename,count(1) from emp group by ename with rollup;
+-------+----------+
| ename | count(1) |
+-------+----------+
| ttx2 | 1 |
| zzx1 | 2 |
| NULL | 3 |
+-------+----------+
3 rows in set (0.00 sec)
#统计出生人数同时统计总人数
mysql> select birth,count(1) from emp group by birth with rollup;
+------------+----------+
| birth | count(1) |
+------------+----------+
| 2000-01-01 | 1 |
| 2002-03-09 | 1 |
| 2023-04-10 | 1 |
| NULL | 3 |
+------------+----------+
4 rows in set (0.00 sec)
#统计录用时间同时统计总人数
mysql> select hirdate,count(1) from emp group by hirdate with rollup;
+------------+----------+
| hirdate | count(1) |
+------------+----------+
| 2000-01-01 | 1 |
| 2009-04-03 | 1 |
| 2010-03-04 | 1 |
| NULL | 3 |
+------------+----------+
4 rows in set (0.00 sec)
#统计薪资数同时统计总人数
mysql> select sal,count(1) from emp group by sal with rollup;
+---------+----------+
| sal | count(1) |
+---------+----------+
| 2000.00 | 1 |
| 2001.00 | 1 |
| 4000.00 | 1 |
| NULL | 3 |
+---------+----------+
4 rows in set (0.00 sec)
#统计年龄同时统计总人数
mysql> select age1,count(1) from emp group by age1 with rollup;
+------+----------+
| age1 | count(1) |
+------+----------+
| 21 | 1 |
| 22 | 1 |
| 23 | 1 |
| NULL | 3 |
+------+----------+
4 rows in set (0.00 sec)
#统计部门大于0的部门
mysql> select deptno,count(1) from emp group by deptno having count(1)>0;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
+--------+----------+
3 rows in set (0.00 sec)
#计算薪资总数、最高薪资、最低薪资
mysql> select sum(sal),max(sal),min(sal) from emp;
+----------+----------+----------+
| sum(sal) | max(sal) | min(sal) |
+----------+----------+----------+
| 8001.00 | 4000.00 | 2000.00 |
+----------+----------+----------+
1 row in set (0.00 sec)
#联表查询
mysql> create table dept(deptno int(10),deptname varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into dept values(1,'tech');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dept values(2,'sale');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dept values(3,'hr');
Query OK, 1 row affected (0.00 sec)
mysql> select ename from emp where emp.deptno=emp.deptno;
+-------+
| ename |
+-------+
| zzx1 |
| zzx1 |
| ttx2 |
+-------+
3 rows in set (0.00 sec)
#左连接查询
mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
+-------+----------+
| ename | deptname |
+-------+----------+
| zzx1 | tech |
| zzx1 | hr |
| ttx2 | NULL |
+-------+----------+
3 rows in set (0.00 sec)
#右连接查询
mysql> select ename,deptname from dept right join emp on dept.deptno=emp.deptno;
+-------+----------+
| ename | deptname |
+-------+----------+
| zzx1 | tech |
| zzx1 | hr |
| ttx2 | NULL |
+-------+----------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 3 | hr |
+--------+----------+
3 rows in set (0.01 sec)
mysql> insert into dept values(4,'sl');
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values('ssss','2019-01-01','2018-01-01',5000,2,24);
Query OK, 1 row affected (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 3 | hr |
| 4 | sl |
+--------+----------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
| ssss | 2019-01-01 | 2018-01-01 | 5000.00 | 2 | 24 |
+-------+------------+------------+---------+--------+------+
4 rows in set (0.00 sec)
mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
+-------+----------+
| ename | deptname |
+-------+----------+
| zzx1 | tech |
| ssss | sale |
| zzx1 | hr |
| ttx2 | sl |
+-------+----------+
4 rows in set (0.00 sec)
mysql> select ename,deptname from dept right join emp on dept.deptno=emp.deptno;
+-------+----------+
| ename | deptname |
+-------+----------+
| zzx1 | tech |
| ssss | sale |
| zzx1 | hr |
| ttx2 | sl |
+-------+----------+
4 rows in set (0.00 sec)
MySQL-SQL基础1的更多相关文章
- 2.Mysql SQL基础
2.Mysql SQL基础2.1 SQL简介 SQL(Structure Query Language)是结构化查询语言.2.2 SQL使用入门 2.2.1 SQL分类 SQL分为DDL.DML(DQ ...
- 3 MySQL SQL基础
目录 1. SQL概述2. 数据库操作3. 表操作4. 记录操作 1. SQL概述 SQL,结构化查询语言(Structured Query Language),一种数据库查询和程序设计语言,用于存取 ...
- mysql sql 基础总结
1 mysql top n使用 select * from table limit n; 2 统配符使用必须和like结合使用 like % 通配符 描述 % 替代一个或多个字符 _ 仅替代一个 ...
- mysql使用基础 sql语句(一)
csdn博文地址:mysql使用基础 sql语句(一) 点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...
- MySQL基础整理(一)之SQL基础(未完成)
大家好,我是浅墨竹染,以下是MySQL基础整理(一)之SQL基础 1.SQL简介 SQL(Structure Query Language)是一种结构化查询语言,是使用关系模型的数据库应用语言. 2. ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
- sql 基础练习 计算7天各个时间点的总和 group by order mysql一次查询多个表
SQL 基础练习 -- 创建数据库 CREATE DATABASE school CHARACTER SET UTF8; -- 使用数据库 USE school; -- id: 学生的id -- na ...
- (2.16)Mysql之SQL基础——函数
(2.16)Mysql之SQL基础——函数 关键词:mysql函数,mysql自定义函数,mysql聚合函数,mysql字符串函数,mysql数值函数 1.自定义函数 -- (1)一般形式 creat ...
- (2.15)Mysql之SQL基础——开发设计最佳规范
(2.15)Mysql之SQL基础——开发设计最佳规范 关键字:mysql三大范式,mysql sql开发规范 分析: show profile.mysqllsla.mysqldrmpslow.exp ...
- (2.14)Mysql之SQL基础——游标
(2.14)Mysql之SQL基础——游标 关键词:Mysql游标 -- (1)定义游标 declare cur_name cursor for select * from table_name wh ...
随机推荐
- 搭建kerberos和NTP服务器以及安全的NFS服务
说明:这里是Linux服务综合搭建文章的一部分,本文可以作为单独搭建Kerberos和NTP时钟服务的参考. 注意:这里所有的标题都是根据主要的文章(Linux基础服务搭建综合)的顺序来做的. 如果需 ...
- 关于 .NET 与 JAVA 在 JIT 编译上的一些差异
最近因为公司的一些原因,我也开始学习一些 JAVA 的知识.虽然我一直是以 .NET 语言为主的程序员,但是我并不排斥任何其它语言.在此并不讨论 JAVA .NET 的好坏,仅仅是对 .NET 跟 J ...
- xhell、xftp、putty使用教程
作为远程登陆工具,上传代码登陆服务器工具 1.XSHELL Xshell是远程连接Linux服务器的工具,基于SSH协议,使用它可以更加方便的操作Linux操作系统,在刚使用时可能需要提前简单的设置下 ...
- 第一篇 -- Sprint Tool Suite配置和Hello World编写
首先需要安装 1. Sprint Tool Suite(本次所用版本:spring-tool-suite-3.8.3.RELEASE-e4.6.2-win32-x86_64) 2. Tomcat(本次 ...
- Floyd弗洛伊德算法
先看懂如何使用 用Java实现一个地铁票价计算程序 String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A1 ...
- 【阅读笔记】Java核心技术卷一 #3.Chapter5
5 继承 5.1 类.超类和子类 5.1.1 定义子类 超类(superclass)和子类(subclass), 基类(base class)和派生类(derived class), 父类(paren ...
- vscode配置及插件
编辑vue时候的用户配置 { "workbench.colorTheme": "Solarized Dark", // 主题 "editor.dete ...
- 论文笔记:(NIPS2018)PointCNN: Convolution On X-Transformed Points
目录 摘要 一.2D卷积应用在点云上存在的问题 二.解决的方法 2.1 idea 2.2 X-conv算子 2.3 分层卷积 三.实验 3.1分类和分割 3.2消融实验.可视化和模型复杂度 总结 仍存 ...
- Python小白的数学建模课-16.最短路径算法
最短路径问题是图论研究中的经典算法问题,用于计算图中一个顶点到另一个顶点的最短路径. 在图论中,最短路径长度与最短路径距离却是不同的概念和问题,经常会被混淆. 求最短路径长度的常用算法是 Dijkst ...
- 腾讯云TDSQL监控库密码忘记问题解决实战
首先,给大家介绍一下TDSQL.TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用.全球部署架构.分布式水平扩展.高性能.企业级安全等特性, ...