导入*.sql数据到数据库

  • windows系统

    ​ source d:/tables.sql;

  • Linux系统

    source /home/soft/桌面/tables.sql;

  • 导入完成后 测试查询

    show tables; 查询出两个表 emp和dept
    
    select * from emp; 里面会有一堆数据
    
    

is null 和 is not null

  • 当查询字段的值为空值时 不能用等号进行判断,使用is

    1. 查询没有上级领导的员工信息;
    select * from emp where manager is null;
    2. 查询有上级领导的员工信息;
    select * from emp where manager is not null;

别名 --select查询 as

  • 把查询到的员工姓名name 改成名字

  • - select name as '名字' from emp;
    - select name '名字' from emp;
    - seclect 名字 from emp;

去重distinct

1. 查询员工表中出现了哪几种不同的工作
select distinct job from emp;
2. 查询员工表里面有哪几个部门id
select distinct deptId from emp;

比较运算符 > < >= <= = !=和<>

1. 查询工资大于等于3000的员工姓名和工资
select name,sal from emp where sal>=3000;
2. 查询1号部门的员工姓名和工作
select name,job from emp where deptId=1;
3. 查询不是程序员的员工姓名,工资和工作 (用到上面两种不等的写法)
select name,sal,job from emp where job!='程序员';
select name,sal,job from emp where job<>'程序员';
4. 查询有奖金的员工姓名和奖金
select name,comm from emp where comm>0;

逻辑运算符(自定义)

- and / or / not 与或非

  • and 类似Java中的 &&

  • or 类似Java中的||

  • not 类似Java中的!

  • 1. 查询1号部门工资高于2000块钱的员工信息
    select * from emp where deptId=1 and sal>2000;
    2. 查询是程序员或者工资等于5000的员工信息
    select * from emp where job='程序员' or sal=5000;
    3. 查询出CEO和项目经理的名字
    select name from emp where job='CEO' or job='项目经理';
    4. 查询出奖金为500并且是销售的员工信息 select * from emp where comm=500 and job='销售'

in关键字

  • 当查询某个字段的值为多个值的时候使用

  • - 
    
    1. 查询出工资为3000,1500和5000的员工信息
    select * from emp where sal=3000 or sal=1500 or sal=5000;
    select * from emp where sal in(3000,1500,5000);
    2. 查询工资不是3000,1500和5000的员工信息
    select * from emp where sal not in(3000,1500,5000);
    3. 查询1号和2号部门工资大于2000的员工信息
    select * from emp where deptId in(1,2) and sal>2000;

between x and y

  • 查询数据在两者之间使用 , 包含x和y

    1. 查询工资在2000到3000之间的员工信息
    
    select * from emp where sal>=2000 and sal<=3000;
    
    select * from emp where sal between 2000 and 3000;
    
    2. 查询工资在2000到3000之外的员工信息
    
    select * from emp where sal not between 2000 and 3000;

模糊查询like

  • _: 代表1个未知字符
  • %: 代表0或多个未知字符
  • 举例:
    • 以x开头 x%
    • 以x结尾 %x
    • 包含x %x%
    • 第二个字符是x _x%
    • 第三个是x倒数第二个是y _ _ x%y _
练习1:
查询姓孙的员工信息 select * from emp where name like "孙%"; 查询工作中第二个字是售的员工信息 select * from emp where job like "_售%"; 查询名字中以精结尾的员工姓名 select name from emp where name like '%精'; 查询名字中包含僧的员工并且工资高于2000的员工信息 select * from emp where name like '%僧%' and sal>2000; 查询1号和2号部门中工作以市开头的员工信息 select * from emp where deptId in(1,2) and job like '市%'; 查询有领导的员工中是经理的员工姓名 select name from emp where manager is not null and job like '%经理%';
练习2:
select * from t_item where title like '%记事本%'and price<100; select * from t_item where title like '得力' and price between 50 and 100; select * from t_item where image is not null; select * from t_item where category_id in(238,917); select * from t_item where sellpoint '%赠%'; select title from t_item where title not like '%得力%'; select * from t_item where price not between 50 and 200;

排序 order by

  • 格式: order by 排序字段名 asc升序( 默认 ) 或 desc降序

  • 多字段排序 : order by 字段名1 asc/desc,字段名2 asc/desc;

    1. 查询所有员工的姓名和工资并安装工资升序排序
    select name,sal from emp order by sal; 2. 查询所有员工的姓名和工资并安装工资降序排序
    select name,sal from emp order by sal desc; 3. 查询所有员工姓名,工资和部门编号 , 安装部门编号升序排序,如果部门编号一致则按照工资降序排序
    select name,sal,deptid from emp order by deptid ,sal desc;

分页查询limit

  • 格式: limit 跳过的条数,请求的条数(每页的条数)
  • 跳过的条数=(请求的页数-1)*每页的条数
  • 举例 :
    • 查询第一页的10条数据 limit 0,10
    • 查询第二页的10条数据 limit 10,10
    • 查询第5页的10条数据 limit 40,10
    • 查询第8页的5条数据 limit 35,5
    • 查询第7页的9条数据 limit 54,9
工资升序排序 查询前三名

select * from emp order by sal limit 0,3;

查询员工表中工资降序排序 第二页的3条数据

select * from emp order by sal desc limit 3,3;

查询1号部门中工资最高的员工信息

select * from emp where deptId=1 order by sal desc limit 0,1;

查询销售相关工作里面赚钱最少的员工姓名和工资

select name,sal from emp where job like '%销售%'

order by sal limit 0,1;

按照工资降序排序查询工资高于1000的所有员工姓名和工资, 查询第三页的两条数据

select name,sal from emp where sal>1000 order by sal desc limit 4,2;

数值计算 + - * / %

  • 7%2 等效于 mod(7,2)
1. 查询每个员工的姓名,工资和年终奖(年终奖=5*月工资)
select name,sal,sal*5 from emp;
//select price,num,price*num 总金额 from t-item; 2.查询2号部门中的员工姓名,工资和涨薪5块钱之后的工资
select name,sal+5 from emp; 3.让员工表中3号部门的员工每人涨薪5块钱
uddate emp set sal=sal+10;
- 改回去
upate emp set sal=sal-10

08 MySQL_SQL_DQL_select数据查询条件判断的更多相关文章

  1. Angular4.x 创建组件|绑定数据|绑定属性|数据循环|条件判断|事件|表单处理|双向数据绑定

    Angular4.x 创建组件|绑定数据|绑定属性|数据循环|条件判断|事件|表单处理|双向数据绑定 创建 angular 组件 https://github.com/angular/angular- ...

  2. js 树结构数据遍历条件判断

    代码: /** * 树结构数据条件过滤 * js 指定删除数组(树结构数据) */ function filter (data, id) { var newData = data.filter(x = ...

  3. 取得数据库中数据 查询条件where使用规则

    string where = string.Format("DnX < {0} and DnD > {0} and Types = '{1}' and Type1 = '{2}' ...

  4. MySQL多表数据查询(DQL)

    数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...

  5. MYSQL数据类型和where条件判断

    MySQL中常见的数据类型 一.字符型 ① CHAR(N):固定N个字符长度的字符串,如果长度不够自动空格补齐; N的范围 0~255 ② VARCHAR(N): 存储可变长度的字符串,最常用 ③ T ...

  6. MySql数据查询的逻辑蕴含条件问题

    SQL语言中没有蕴含逻辑运算.但是,可以利用谓词演算将一个逻辑蕴含的谓词等价转换为:p->q ≡┐p∨q. 我们通过一个具体的题目来分析:(具体的表和数据详见文章:Mysql数据库中的EXIST ...

  7. mysql中运用条件判断筛选来获取数据

    ### part1 单表查询 sql查询完整语法: select .. from .. where .. group by .. having .. order by .. limit .. 一.wh ...

  8. 利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理

    在Asp.net Web API中,对业务数据的分页查询处理是一个非常常见的接口,我们需要在查询条件对象中,定义好相应业务的查询参数,排序信息,请求记录数和每页大小信息等内容,根据这些查询信息,我们在 ...

  9. easyUI datagrid 根据查询条件 选中对应数据的行

    开始 输入了 土豆,南瓜,再次是小青菜,每次输入点击搜索的时候(模糊查询),选中的当前数据对应的行 在做之前,在网上查询了许多资料,也在技术群里问过许多次,弄了好久终于好了. 第一次写博客真不知道写啥 ...

随机推荐

  1. windows使用命令行终止端口的进程

    C:\Users\fxz>netstat -ano | find "8093" TCP 0.0.0.0:8093 0.0.0.0:0 LISTENING 3956 TCP [ ...

  2. Linux磁盘分区-mount挂载

    Linux磁盘分区类型 磁盘存储术语CHS head:磁头  磁头数=盘面数 track:磁道  磁道=柱面数 sector:扇区,512bytes cylinder:柱面 1柱面=512*secto ...

  3. [Istio是什么?] 还不知道你就out了,一文40分钟快速理解

    @ 目录 前言 Istio是什么? 服务网格是什么? 应用场景 为什么使用Istio? 流量管理介绍 istio架构 Envoy Sidecar Istiod 虚拟服务(VirtualService) ...

  4. 基于dhtmlxGantt的Blazor甘特图组件

    基于dhtmlxGantt实现的甘特图组件,目前仅做到了数据展现,方法及插槽暂未实现,若需可按照dhtmlxGantt的文档及微软的Balzor文档,自行扩展. 数据发生变化后甘特图会立即发生变化. ...

  5. python常用标准库(math数学模块和random随机模块)

    常用的标准库 数学模块 import math ceil -- 上取整 对一个数向上取整(进一法),取相邻最近的两个整数的最大值. import math res = math.ceil(4.1) p ...

  6. Property or method "xxx" is not defined on the instance but referenced during render

    是xxx中的data写成date了,因此报错. 这个错误属于粗心

  7. Python模块Ⅰ

    Python模块Ⅰ part1 模块的定义/取别名 自定义模块 什么是模块:模块的本质就是.py文件,封装语句的最小单位 模块中出现的变量,for循环,if结构,函数定义...称为模块成员 模块的运行 ...

  8. Mac下最好用的SSH连接客户端 (Termius)

    Termius是微软的一款SSH终端工具,它支持多平台.而且操作界面十分ha好看且简洁,今天分享给大家️ 软件下载 关注下方公众号,回复termius获取下载地址   软件功能介绍 Termius M ...

  9. LC T668笔记 & 有关二分查找、第K小数、BFPRT算法

    LC T668笔记 [涉及知识:二分查找.第K小数.BFPRT算法] [以下内容仅为本人在做题学习中的所感所想,本人水平有限目前尚处学习阶段,如有错误及不妥之处还请各位大佬指正,请谅解,谢谢!] !! ...

  10. 最强肉坦:RUST多线程

    Rust最近非常火,作为coder要早学早享受.本篇作为该博客第一篇学习Rust语言的文章,将通过一个在其他语言都比较常见的例子作为线索,引出Rust的一些重要理念或者说特性.这些特性都是令人心驰神往 ...