今日内容 表查询关键字 详细讲解

  • *操作表的SQL语句补充

  • *表查询关键字

首先创建一张数据表进行操作可以更好的理解:

示例:
1. 1.查询id大于等于3小于等于6的数据
select * from epm where id between 3 and 6;  # 全表查询

+—-+——-+——–+—–+————+———+————–+———+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+——-+——–+—–+————+———+————–+———+——–+———–+
| 3 | kevin | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | tony | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | owen | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jack | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
+—-+——-+——–+—–+————+———+————–+———+——–+———–+

select id,name from where id >=3 and id6; # 精确查询

+—-+——-+
| id | name |
+—-+——-+
| 3 | kevin |
| 4 | tony |
| 5 | owen |
| 6 | jack |
+—-+——-+

2.查询薪资是20000或者18000或者17000的数据
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,180000,17000); # 简写

+—-+———–+——–+—–+————+———–+————–+———-+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+———–+——–+—–+————+———–+————–+———-+——–+———–+
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+—-+———–+——–+—–+————+———–+————–+———-+——–+———–+

3.查询员工姓名中包含o字母的员工姓名和薪资
select name,salary from emp where name like '%o%';

+——-+————+
| name | salary |
+——-+————+
| jason | 7300.33 |
| tom | 1000000.31 |
| tony | 3500.00 |
| owen | 2100.00 |
+——-+————+

4.查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;

+——+———-+
| name | salary |
+——+———-+
| tony | 3500.00 |
| owen | 2100.00 |
| jack | 9000.00 |
| sank | 10000.00 |
+——+———-+

5.查询id小于3或者大于6的数据
select * from emp where id not between 3 and 6;

6.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);

7.查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is
select name,post from emp where post_comment = NULL;  # 查询为空!
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;
限制展示条数
select * from emp limit 3;

+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+
| 1 | jason | male | 18 | 2017-03-01 | 浦东第一帅形象代言 | NULL | 7300.33 | 401 | 1 |
| 2 | tom | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | kevin | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+

查询工资最高的人的详细信息
select * from emp order by salary desc limit 1;

+—-+——+——+—–+————+———+————–+————+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+——+——+—–+————+———+————–+————+——–+———–+
| 2 | tom | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
+—-+——+——+—–+————+———+————–+————+——–+———–+

分页显示
select * from emp limit 0,5;  # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置

+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+
| 1 | jason | male | 18 | 2017-03-01 | 浦东第一帅形象代言 | NULL | 7300.33 | 401 | 1 |
| 2 | tom | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | kevin | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | tony | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | owen | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
+—-+——-+——+—–+————+—————————–+————–+————+——–+———–+

select * from emp limit 5,5;

+—-+——–+——–+—–+————+———+————–+———-+——–+———–+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+—-+——–+——–+—–+————+———+————–+———-+——–+———–+
| 6 | jack | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jenny | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | sank | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 哈哈 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 呵呵 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
+—-+——–+——–+—–+————+———+————–+———-+——–+———–+

通配符 说明

指定数目的匹配

不少于指定数目的匹配

  • *多表查询思路

Original: https://www.cnblogs.com/tai-yang77/p/16596310.html
Author: 你好你好你好丶
Title: 今日内容 表查询关键字 详细讲解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/602939/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球