美文网首页
MySQL中的查询-where

MySQL中的查询-where

作者: djz0306 | 来源:发表于2019-06-28 20:17 被阅读0次

在开始本篇之前需要准备较多的数据。本系列是按照燕十八老师的内容和顺序整理的。练习所需的数据库按照这篇文章中的说明建立。表的结构如图:

goods表.jpg

where 匹配

运算符

比较运算符

  1. 查询主键为 32 的商品(=):

    select goods_id,goods_name,shop_price from goods where goods_id = 32;
    
  2. 查询不属于第 3 个栏目的所有商品(!= <>):

    不等于使用 != 或者 <>

    select goods_id,cat_id,goods_name,shop_price from goods where cat_id!=3;
    
  3. 本店价格高于 3000 的商品(>):

    select goods_id,goods_name,shop_price from goods where shop_price > 3000;
    
  4. 本店价格低于或100元的商品(<=):

    select goods_id,goods_name,shop_price from goods where shop_price <= 100;
    
  5. 在不使用 or 的情况下,取出第 4 栏目和第 11 栏目的所有商品( in ):

    select goods_id,cat_id,goods_name,shop_price from goods where cat_id in (4,11);
    
  6. 在不使用 and 的情况下,取出价格在 100<=价格<=500 的商品(between...and):

    select goods_id,goods_name,shop_price from goods where shop_price between 100 and 500;
    

    in 表示在集合 (4,11) 中,between表示在 4-11 之间,包括边界值

逻辑运算符

  1. 取出不属于第 3 个栏目且不属于第 11 个栏目的商品,用 not in 和 and 分别实现:

    select goods_id,cat_id,goods_name,shop_price from goods where cat_id not in (3,11);
    
    select goods_id,cat_id,goods_name,shop_price from goods where cat_id!= 3 and cat_id!=11;
    
  2. 取出价格大于 100 且小于 300,或者大于 4000 且小于 5000 的商品(and 、or):

    select goods_id,goods_name,shop_price from goods where shop_price > 100 and shop_price < 300 or shop_price > 4000 and shop_price < 5000;
    

    MySQL 中 and 的优先级比 or 高

  3. 取出第三个栏目下价格小于 1000 或者大于 3000,且点击量大于 5 的系列商品:

    select goods_id,cat_id,goods_name,shop_price,click_count from goods where cat_id = 3 and click_count >= 5 and (shop_price < 1000 or shop_price > 3000);
    

模糊查询

  1. 取出所有以“诺基亚”开头的商品:

    select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚%';
    
  2. 取出名字以“诺基亚N”开头,后面只有两个字符的商品:

    select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚N__';
    

    % 通配任意字符,_ 通配单个字符

相关文章

  • MySQL中的查询-where

    在开始本篇之前需要准备较多的数据。本系列是按照燕十八老师的内容和顺序整理的。练习所需的数据库按照这篇文章中的说明建...

  • 查询性能优化

    MySQL查询优化器的局限性 关联子查询 MySQL的子查询实现的非常糟糕,最糟糕的一类查询是where条件中包含...

  • mysql 查询

    mysql的查询、子查询及连接查询 一、mysql查询的五种子句 where(条件查询)、having(筛选)、g...

  • MySQL的有用的时间处理,最近24小时

    mysql中如何查询最近24小时where visittime >= NOW() - interval 1 ho...

  • MySQL中where条件中IN的慢查询优化

    MySQL中where条件中IN的慢查询优化 我们在编写SQL查询语句时,有时候会遇到连表查询的情况,有时的业务场...

  • mysql-字段别名

    mysql中的where子句不能使用字段别名 查询姓名和年薪 查询姓名和年薪按年薪大于100000的

  • mysql的查询、子查询及连接查询

    一、mysql查询的五种子句 where(条件查询)、having(筛选)、group by(分组)、order ...

  • 面试总结

    1.Java编程 2. Mysql添加索引后查询速度没有变化 mysql查询语句的where条件字段和select...

  • mysql执行原理 索引 锁简介

    mysql走你~~ select * form table where id=? 一条mysql查询都会经历些...

  • mysql 中模糊查询

    MySQL中 使用like关键字进行模糊查询,like关键字通常用在where条件查询子句中,like字句通常配合...

网友评论

      本文标题:MySQL中的查询-where

      本文链接:https://www.haomeiwen.com/subject/vffycctx.html