MySQL条件查询:模糊查询:like,between and,in,

模糊查询:
关键字:like

特点:①一般结合通配符使用:
% 任意数量字符,包含0个
_(下划线)任意单个字符

案例1.查询员工名中含字符a的员工信息

   mysql> select *from 
    -> employees
    -> where last_name like '%a%'; 

案例2.查询员工名中第三个字符为n,第五个字符为l的员工名和工资。

mysql> select last_name,salary
    -> from employees
    -> where last_name like '__n_l%';
+-----------+---------+
| last_name | salary  |
+-----------+---------+
| Hunold    | 9000.00 |
+-----------+---------+
1 row in set (0.04 sec)

案例3.查询员工名中第二个字符为_的员工名。需要用到转义符(\)

mysql> select last_name
    -> from employees
    -> where last_name like '_\_%';
+-----------+
| last_name |
+-----------+
| K_ing     |
| K_ing     |
+-----------+

或者直接用ESCAPE定义转义符

mysql> select last_name
    -> from employees
    -> where last_name like '_$_%' escape '$';
+-----------+
| last_name |
+-----------+
| K_ing     |
| K_ing     |
+-----------+

关键字:between and
特点:①包含连接值,如100<=值<=120,输出的值包含100和120
案例1.查询员工编号在100到120之间的员工信息

mysql> select *from
    -> employees
    -> where employee_id between 100 and 120;

使用运算符方法较为繁琐

mysql> select *from 
    -> employees
    -> where employee_id>=100 and employee_id<=120;

关键字:in类似于or或,格式是:in(?,?,?..);
含义:判断某字段的值是否属于in列表中的某一项
特点:in列表的值类型必须一致或兼容
案例1.查询员工的工种编号是IT_PROG、AD_VP、AD_pres中的一个的员工名和工种编号

mysql> select last_name,job_id
    -> from employees
    -> where job_id in('IT_PROT','AD_VP','AD_preS');
+-----------+---------+
| last_name | job_id  |
+-----------+---------+
| K_ing     | AD_PRES |
| Kochhar   | AD_VP   |
| De Haan   | AD_VP   |
+-----------+---------+

关键字:is null 和 is not null

案例1.查询没有奖金的员工名和奖金率
=和<>不能判断是否为null

mysql> select last_name ,commission_pct
    -> from employees
    -> where commission_pct is null;
+-------------+----------------+
| last_name   | commission_pct |
+-------------+----------------+
| K_ing       | NULL           |
| Kochhar     | NULL           |
| De Haan     | NULL           |

案例2.查询奖金的员工名和奖金率

mysql> select last_name ,commission_pct
    -> from employees
    -> where commission_pct is not null;
+------------+----------------+
| last_name  | commission_pct |
+------------+----------------+
| Russell    |           0.40 |
| Partners   |           0.30 |
| Errazuriz  |           0.30 |
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值