MySQL学习(五)
查询数据的学习与练习
建立一个表
CREATE TABLE goods (
`goos_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 (`goos_id`)
) ENGINE MyISAM CHARSET utf8;
插入以下数据

创建表的描述如下

1
2
mysql> select goods_name,cat_id from goods where cat_id!=3;
或者
mysql> select goods_name,cat_id from goods where cat_id<>3;
3
mysql> select goos_id,goods_name from goods where shop_price>3000;
4
mysql> select goos_id,goods_name,shop_price from goods where shop_price<=100;
5取出第4栏目和第11栏目的商品,不能用or
mysql> select goos_id,cat_id,goods_name from goods where cat_id in (4,11);
6
mysql> select goos_id,cat_id,goods_name,shop_price from goods where shop_price
-> between 100 and 500;
between...and是范围查询,端点值也包括在内。
in是离散变量,是散点集合。between...and表示的是一个范围,是连续变量。
7 取出不在
mysql> select goos_id,cat_id,goods_name from goods
-> where cat_id not in(3,11);
用and来实现
mysql> select goos_id,cat_id,goods_name from goods
-> where
-> cat_id != 3 and cat_id != 11;
8
mysql> select goos_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高,但是建议使用括号
9
mysql> select goos_id,goods_name,shop_price,click_count from goods
-> where
-> (cat_id = 3) and (shop_price < 1000 or shop_price < 3000) and (click_count > 5);
10
查出名称以诺基亚开头的商品,如诺基亚N95等等
mysql> select goods_name from goods
-> where
-> goods_name
-> like '诺基亚%';
like模糊查询,%可以匹配任意的字符。用%匹配的范围比较广。
11
使用’_‘匹配任意单个字符
mysql> select goods_name from goods
-> where
-> goods_name
-> like '诺基亚N__';
12
把列看成变量,把where后面看成PHP中if(exp)中exp表达式。哪一行能被取出来?哪一行能满足exp为真,哪一行就被取出来。
把列看程变量,既然是变量,变量之间就可以运算。
mysql> select goos_id,goods_name,market_price,shop_price from goods
-> where
-> (shop_price-market_price) > 0;
计算出来的结果成为"广义投影"(列运算的结果)。
列运算的结果,可以当成列来看,还可以起一个别名。
13
mysql> select goods_name,shop_price,market_price,(shop_price - market_price) as discount
-> from goods
-> where
-> (shop_price - market_price) > 200;
一种典型的错误
mysql> select goods_name,shop_price,market_price,(shop_price - market_price) as discount
-> from goods
-> where
-> discount > 200;
ERROR 1054 (42S22): Unknown column 'discount' in 'where clause'
where发挥作用时,表上并没有discout列,where发挥完作用,形成的结果里才有discount,但此时where已经不能在发挥作用。对于结果中的列,如果还想再次筛选,需要用having。
14 一道面试题
有如下表和数组,把num值处于[20,29]之间的,改为20,把num值位于[30,39]之间的改为30。(要求用一条语句完成)
把num当成变量看!,可以把num/10取整,再乘以10
mysql> update test4
-> set num = floor(num/10)*10
-> where
-> (num > 20 and num <39) ;
15练习题(P18)
把good表中商品名为‘诺基亚XXX’的商品,改为'HTCXXX'
MySQL学习(五)的更多相关文章
- MySql学习(五) —— 数据库优化理论篇(一)
一.数据库管理系统 数据库管理系统(Database Management System, DBMS) 衡量是否是数据库的标准: ACID:是指在数据库管理系统(DBMS)中事务所具有的四个特性: 1 ...
- mysql实体关系(mysql学习五)
实体关系 表设计 1:1 两个实体表内,存在相同的主键字段 如果记录的主键值等于另一个关系表内记录的主键值,则两条记录的对应为一一对应 优化上称为垂直分割 1:n 一个实体对应多个其他实体(一个班级 ...
- MySQL 学习五 SQL实用函数
0 select now() 显示当前时间. 1 select char_length('andyqan') 显示字符长度. 2 日期格式化 select date_format( ...
- MySQL学习(五)——使用JDBC完成用户表CRUD的操作
通过案例我们发现“获得连接”和“释放资源”两次代码将在之后的增删改查所有功能中都存在,开发中遇到此种情况,将采用工具类的方法进行抽取,从而达到代码的重复利用. 1.使用properties配置文件 开 ...
- MySQL学习(六)——自定义连接池
1.连接池概念 用池来管理Connection,这样可以重复使用Connection.有了池,我们就不用自己来创建Connection,而是通过池来获取Connection对象.当使用完Connect ...
- 我的MYSQL学习心得(五) 运算符
我的MYSQL学习心得(五) 运算符 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- 我的MYSQL学习心得(十五) 日志
我的MYSQL学习心得(十五) 日志 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- 别人的的MYSQL学习心得(十五) 日志
我的MYSQL学习心得(十五) 日志 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- 我的MYSQL学习心得(五)
原文:我的MYSQL学习心得(五) 我的MYSQL学习心得(五) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL ...
随机推荐
- python pip
如果pip的版本较低,可能导致pip时安装出错,所以我们要更新pip版本-- 查询pip版本 pip -V -- Linux and OS X 升级 pip install -U pip -- Win ...
- gdb远程debug A syntax error in expression, near `variable)'.
今天调试有个linux环境的应用时,gdb提示A syntax error in expression, near `variable)'.,最后经查,gdb版本过低(比如7.2)或者源代码不匹配所致 ...
- 叶亚明:合格CTO的六要素(转)
叶亚明,携程旅行网CTO & 高级技术副总裁,负责携程的移动.Online.呼叫中心等的技术架构.开发及运营.在加入携程之前,叶亚明是ebay.com技术平台总监,领导ebay.com几代网站 ...
- Centos 7 安装 Supervisor 及使用
Supervisor官网链接:http://supervisord.org/installing.html 安装与设置开机启动: http://blog.csdn.net/fenglailea/art ...
- bzoj 3670 动物园 - kmp - 动态规划
Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...
- powershell的stable和preview版本
在看https://github.com/PowerShell/PowerShell/releases的时候发现,已经发布了6.2.0的preview版本的情况下,还会继续发布6.1.3. 在Read ...
- 题解——CodeForces 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- (转载)C#:Enum、Int和String的互相转换,枚举转换
Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...
- NOI 2011 阿狸的打字机(AC自动机+主席树)
题意 https://loj.ac/problem/2444 思路 多串匹配,考虑 \(\text{AC}\) 自动机.模拟打字的过程,先建出一棵 \(\text{Trie}\) 树,把它变成自动机 ...
- cas4.2.4 登添加验证码
看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 --> &l ...