老师下了ecshop,我看有弹幕同学说:“可以下,但没必要”,我就没下。

查询:

#先模拟ecshop建一个小型的商品表
create table goods(
goods_id mediumint(8) unsigned not null auto_increment,
cat_id smallint(5) unsigned not null default '0',
goods_sn varchar(60) not null default '',
goods_name varchar(120) not null default '',
click_count int(10) unsigned not null default '0',
goods_number smallint(5) unsigned not null default '0',
market_price decimal(10,2) unsigned not null default '0.00',
shop_price decimal(10,2) unsigned not null default '0.00',
add_time int(10) unsigned not null default '0',
is_best tinyint(1) unsigned not null default '0',
is_new tinyint(1) unsigned not null default '0',
is_hot tinyint(1) unsigned not null default '0',
primary key(goods_id)
)engine myisam charset utf8;

查询商品主键是32 的商品:

select goods_id,goods_name,shop_price from goods where goods_id = 32;

查出不属于第三个栏目的所有商品:

#即商品栏目cat_id不等于3
select goods_id,cat_id,goods_name from goods where cat_id != 3;# !=还有另一个写法:<>   这两句语句等价。
select goods_id,cat_id,goods_name from goods where cat_id <> 3;

查出本店价格高于3000元的商品:

select goods_id,goods_name,shop_price from goods where shop_price>3000;

查出本店价格小于或等于100元的商品:

select goods_id, goods_name,shop_price from goods where shop_price<=100;

取出第4个栏目和第11个栏目的商品,且不能用or:

select goods_id,cat_id,goods_name from goods
where cat_id in (4,11);
#表示cat_id在(4,11)这个集合里都满足。

取出商品价格在100和500之间的商品,且不能用and:

select goods_id, goods_name,shop_price from goods where shop_price
between 100 and 500;  #between and 包括边界值(前后边界都包含)!

观察上述两例:

  1. name in (a,d,c,f) 是指 name 值只要在(a,d,c,f)这个集合中,就满足条件。
  2. between and 是一个范围,in 是一个散点集合。

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

select goods_id, cat_id,goods_name from goods
where cat_id not in(3,11);
#用and 来写:
select goods_id,cat_id,goods_name from goods
where cat_id != 3 and cat_id !=11;

取出价格大于100且小于300,或者大于4000且小于5000的商品:

select goods_id,cat_id,goods_name,shop_price from goods
where shop_price>100 and shop_price < 300
or shop_price >4000 and shop_price < 5000;
#由此可知,and 的优先级比 or 的优先级要高!

取出第3个栏目下,价格小于1000或者大于3000,同时点击量大于等于5的商品:

select goods_id,goods_name,shop_price,cat_id,click_count from goods
where cat_id = 3
and (shop_price <1000 or shop_price >3000) and click_count >=5;
#重点:由于优先级的问题,在中间价格的判断中要多加一个括号,保证逻辑语言的准确性。

查出名称以 诺基亚 开头的商品:

#模糊查询!
select goods_id,goods_name from goods
where goods_name like '诺基亚%';
# 若逛商场时,看了一款手机,诺基亚Nxx系列,具体型号记不清了,请查出:
select goods_id,goods_name from goods
where goods_name like '诺基亚__';
#此时把 % 换成两个_(下划线)即可。

注意上面是两个下划线!!!

like 模糊查询:
% 通配任意字符 _ 通配单一字符

MySQL燕十八老师课程笔记:第六课:商品表的各种按条件查询相关推荐

  1. MySQL燕十八老师课程笔记:第九课:having筛选

    回顾之前做过的一道题:查询本店价比市场价省的钱,并且要求省钱200元以上的取出来 select goods_id,market_price,shop_price,(market_price-shop_ ...

  2. MySQL燕十八老师课程笔记:第二课:增删改查

    增: tee D:\1010.sql 回车后显示:Logging to file 'D:\1010.sql' 这句话是把敲的sql及结果都输出到一个sql文件里,便于复习. create table ...

  3. MySQL燕十八老师课程笔记:第十六课:MySQL各个函数

    # 在给商品打八八折的基础上抹掉零头 select goods_id,goods_name,floor(shop_price*0.88) from goods where cat_id=4; # 给每 ...

  4. MySQL燕十八老师课程笔记:第十五课:union

    union:合并两条或多条语句的结果. 语法:sql1 union sql2 # 要求查出价格低于100元和价格高于4000元的商品,要求不能用or# 先查低于100元的商品 select goods ...

  5. MySQL燕十八老师课程笔记:第五课:建表

    MySQL中,Boolean型就是tinyint. 建表案例:创建某高端白领私密社交网站.username.id.gender.weight.birth.salary.lastlogin. 除了use ...

  6. MySQL燕十八老师课程笔记:第十七课:视图

    # 查询每个栏目下商品的平均价格,并取平均价前3高的栏目 select cat_id,avg(shop_price) as pj from goods group by cat_id order by ...

  7. MySQL燕十八老师课程笔记:第十九课:事务

    什么是事务? 将一个业务下的SQL语句作为一个单元统一操作==>"同生共死"[myisam不支持事务] 例如:A转账500给B,打完之后A减少500,B增加500,如果这两个 ...

  8. MySQL燕十八老师课程笔记:第十一课:子查询

    # 查出本网站最新的(goods_id)最大的一条商品 # 思路:按goods_id desc排序,再取第一行 select goods_id,goods_name from goods order ...

  9. html css燕十八,燕十八_divcss教学笔记.doc

    燕十八_divcss教学笔记 第一天 Html 三部分 1:文档声明 文档声明很重要,直接影响浏览器的渲染效果. 不属于html文档的一部分,不用闭合. 2:head区域 网页的标题 3: body区 ...

最新文章

  1. CCS下DSP仿真实现双边带调制与频谱分析(查表法)
  2. 2018.3.13 12周2次课
  3. POJ 2253 Frogger(最短路 Floyd)
  4. ubuntu18.04.4 LTS 安装NVIDIA驱动亲测有用方法2020年最新及常见问题避坑
  5. dp打开思路4:POJ1189 UVA12511 HDU2845 HBCPC K
  6. html打开显示脚本错误,IE浏览器显示脚本错误怎么办 IE浏览器脚本错误解决方法图文教程...
  7. 计算机工程学院迎新晚会,计算机与信息工程学院2016级迎新晚会举行
  8. 线性代数学习笔记(二)
  9. dll注入的一种方式
  10. esp8266开发入门教程(基于Arduino)——编程基础介绍
  11. PB50打印机测试结果:霍尼韦尔 、intermec 打印机不能买
  12. PLSQL 官方下载及安装
  13. 【FPGA基础】DDR的基本原理介绍,DDR快速上手使用
  14. 区块补习班 | 假酒害人屡禁不止?对不起我来晚了!
  15. 大学英语六级考试大纲 A
  16. pythonplt制作饼状图_4.5Python数据处理篇之Matplotlib系列(五)---plt.pie()饼状图
  17. 单链表的从小到大排序
  18. 数据压缩(一)——音频文件时域频域分析RGB文件三通道的熵
  19. c++期末上机oj题目汇总二(2018北邮信通版)纯干货
  20. WorkManager详解,【面试总结】

热门文章

  1. 蓝牙技术|华为手环7正式发布,智能可穿戴走向新征程
  2. 通过javaMail发送邮件,可选添加多个收件人,密送,抄送,多个附件
  3. 惊心动魄的SAP S4客户额度调整运动
  4. 监听pda扫描_PDA终端扫描实现
  5. 百度云下载神器 速盘SpeedPan v1.9.7
  6. 计算机二维辅助设计课程设计的目的,《计算机辅助设计》课程教学大纲
  7. wave文件格式详解
  8. 影视类的期刊哪些比较好?
  9. python计算折扣价_计算折扣的Python函数
  10. SAP那些事-职业篇-19-论ERP实施效果的评估