Mysql(二)——简单查询及示例

 2023-09-11 阅读 17 评论 0

摘要:简单查询1.查询特定的列2.查询所有的列3.给列起别名(简化列名称)4.显示不同的记录5.查询时执行计算6.查询结果排序7.条件查询 where8.> < >= <= = !=(不等于) is (not) null && ||9.模糊条件查询like 以下查询练习以下表emp(人员

简单查询

      • 1.查询特定的列
      • 2.查询所有的列
      • 3.给列起别名(简化列名称)
      • 4.显示不同的记录
      • 5.查询时执行计算
      • 6.查询结果排序
      • 7.条件查询 where
      • 8.> < >= <= = !=(不等于) is (not) null && ||
      • 9.模糊条件查询like

以下查询练习以下表emp(人员表)和dept(部门表)为例
在这里插入图片描述
在这里插入图片描述

1.查询特定的列

MySQL子查询。查询出所有员工的编号和姓名
select eid,ename from emp;

查询出所有员工的姓名、生日、工资
select ename,birthday,salary from emp;

2.查询所有的列

select eid,ename,sex,birthday,salary,deptId from emp;
select * from emp;

3.给列起别名(简化列名称)

查询出所有员工的编号和姓名,使用汉字别名
select eid as 编号,ename as 姓名 from emp;

Mysql查询,查询出所有员工的姓名、性别、生日、工资,使用一个字母作为别名
select ename a,sex b,birthday c,salary d from emp;
tip:as关键字可以省略,保留即可。

4.显示不同的记录

查询出都有哪些性别的员工
select distinct sex from emp;

查询出员工都分布在哪些部门
select distinct deptId from emp;

5.查询时执行计算

计算 1+3+5+87.4+54.39
select 1+3+5+8*7.4+5*4.39;

简单选择排序,查询出所有员工的姓名及其年薪
select ename,salary*12 from emp;

假设每个员工的工资增长1000,年终奖20000,查询出所有员工的姓名及其年薪,使用汉字别名
select ename 姓名,(salary+1000)*12+20000 年薪 from emp;

6.查询结果排序

  • asc 升序 #ascendant 升序的
  • desc 降序 #descendant 降序

查询出所有的部门,结果按照部门编号升序排列
select * from dept order by did asc;

查询出所有的部门,结果按照部门编号降序排列
select * from dept order by did desc;

MySQL查看数据库,查询出所有的员工,结果按照工资降序排列
select * from emp order by salary desc;

查询出所有的员工,结果按照年龄从大到小排列(生日从小到大)
select * from emp order by birthday asc;

查询出所有的员工,结果按照姓名的升序排列
select * from emp order by ename;
tip:按照字符串排序是按照字符的编码排列
不加排序规则,默认是按照升序排列

查询出所有的员工,结果按照工资的升序排列,要求男员工显示在前女员工显示在后
select * from emp order by sex desc, salary;

MySQL查看表命令、查询出所有的员工,结果按照部门升序排列,如果部门相同按照年龄从小到大排列(生日越小年龄越大)
select * from emp order by deptId,birthday desc;

7.条件查询 where

查询出编号为5的员工所有列
select * from emp where eid=5;

查询出姓名为king的员工所有列
select * from emp where ename='king';

查询出20号部门下的员工有哪些
select * from emp where deptId=20;

MySQL查询语句?查询出工资在6000以上的员工有哪些
select * from emp where salary>6000;

8.> < >= <= = !=(不等于) is (not) null && ||

and (&&) 并且,两个条件都满足
or (||) 或者,两个条件满足其一
is null 值为null
is not null 值不为null
in( ) 满足其中一个
not in( ) 都不满足


查询出不在20号部门下的员工有哪些

select * from emp where deptId!=20;

查询出没有明确部门的员工有哪些
select * from emp where deptId is null;

最简单的php代码示例,查询出有明确部门的员工有哪些
select * from emp where deptId is not null;

查询出工资在7000以上的男员工有哪些
select * from emp where salary>7000 and sex=1;
select * from emp where salary>7000 && sex=1;


a between and b 在a~b之间

a not between and b 不在a~b之间

查询出工资在6000~9000直接的员工有哪些

g代码程序简单示例,select * from emp where salary>=6000 && salary<=9000;
select * from emp where salary between 6000 and 9000;

查询出工资在6000以下或者9000以上的员工有哪些
select * from emp where salary<6000 or salary>9000;
select * from emp where salary<6000 || salary>9000;
select * from emp where salary not between 6000 and 9000;

查询出1993年出生的员工有哪些
select * from emp where birthday>='1993-1-1' && birthday<='1993-12-31';

select * from emp where birthday between '1993-1-1' and '1993-12-31';

MySQL查看表结构。查询出20号部门或者30号部门的员工有哪些
select * from emp where deptId=20 || deptId=30;
select * from emp where deptId in(20,30);

查询出不在20号部门并且不在30号部门的员工有哪些
select * from emp where deptId!=20 && deptId!=30;
select * from emp where deptId not in(20,30);


9.模糊条件查询like

%——匹配任意个字符 >=0
_ ——任意一个字符 =1

查询出姓名中含有字母o的员工有哪些
select * from emp where ename like '%o%';

查询出姓名中以o结尾的员工有哪些
select * from emp where ename like '%o';

查询出姓名中第二个字符是o员工有哪些
select * from emp where ename like '_o%';

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/48859.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息