• MySQL-SQL基础1


     
    
    #插入
    
    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)
    
     
    
     
  • 相关阅读:
    困扰几周了,请教啊,android与websevice数据交互很诡异的问题
    最新版本_adt-bundle-windows-x86_64-20140702 无法建立avd
    android向web提交数据,中文乱码
    activity怎么控制fragment中的textview组件
    关于云储存或者百度云的基础问题, 用java/android 实现上传文件到云储存(比如百度云)
    短信列表如何让同一个号码的短信只显示一条,刚刚加载短信列表会加载所有的数据列。求指教
    Android图片上传到服务器的问题
    安卓模拟器这么慢,大家都怎么调试的?
    浏览器前缀-----[译]Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
    windows 下安装nodejs及其配置环境
  • 原文地址:https://www.cnblogs.com/drizzle-xu/p/10253667.html
Copyright © 2020-2023  润新知