查询数据的学习与练习

建立一个表

 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学习(五)的更多相关文章

  1. MySql学习(五) —— 数据库优化理论篇(一)

    一.数据库管理系统 数据库管理系统(Database Management System, DBMS) 衡量是否是数据库的标准: ACID:是指在数据库管理系统(DBMS)中事务所具有的四个特性: 1 ...

  2. mysql实体关系(mysql学习五)

    实体关系  表设计 1:1 两个实体表内,存在相同的主键字段 如果记录的主键值等于另一个关系表内记录的主键值,则两条记录的对应为一一对应 优化上称为垂直分割 1:n 一个实体对应多个其他实体(一个班级 ...

  3. MySQL 学习五 SQL实用函数

    0 select now() 显示当前时间. 1 select char_length('andyqan')   显示字符长度. 2 日期格式化         select date_format( ...

  4. MySQL学习(五)——使用JDBC完成用户表CRUD的操作

    通过案例我们发现“获得连接”和“释放资源”两次代码将在之后的增删改查所有功能中都存在,开发中遇到此种情况,将采用工具类的方法进行抽取,从而达到代码的重复利用. 1.使用properties配置文件 开 ...

  5. MySQL学习(六)——自定义连接池

    1.连接池概念 用池来管理Connection,这样可以重复使用Connection.有了池,我们就不用自己来创建Connection,而是通过池来获取Connection对象.当使用完Connect ...

  6. 我的MYSQL学习心得(五) 运算符

    我的MYSQL学习心得(五) 运算符 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  7. 我的MYSQL学习心得(十五) 日志

    我的MYSQL学习心得(十五) 日志 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  8. 别人的的MYSQL学习心得(十五) 日志

    我的MYSQL学习心得(十五) 日志 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  9. 我的MYSQL学习心得(五)

    原文:我的MYSQL学习心得(五) 我的MYSQL学习心得(五) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL ...

随机推荐

  1. 过滤特殊字符(包括过滤emoji表情)

    /** * 过滤特殊字符 * @param $text * @return mixed */ public static function filterSpecialChars($text) { // ...

  2. css显示display、可见性visibility、定位position、对齐

    隐藏一个元素可以通过把display属性设置为"none",或把visibility属性设置为"hidden",但是这两种方法会产生不同的结果. display ...

  3. 【题解】Luogu P2147 [SDOI2008]洞穴勘测

    原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我不太会踩爆Link-Cut-Tree的并查集做法qaq 我们用Link-Cut-Tree维护连通性(十分无脑) Co ...

  4. k8s device plugin

    基本概念入门: Device Manager Proposal Device plugin offical Doc(中文) device-plugins offical Doc(En) Go thro ...

  5. openstack components internal relations

    1.  各个组件之间可以互相调用(都是common sense) conductor 负责DB的操作. 各个组件之间通过RPC, 序列化通过oslo_versionedobjects. 2. 具体调用 ...

  6. Bugku-CTF之flag.php(点了login咋没反应)

      Day2   flag.php(点了login咋没反应)   地址:http://123.206.87.240:8002/flagphp/      

  7. xlrd、xlwt 操作excel表格详解

    转自:https://www.cnblogs.com/jiablogs/p/9141414.html python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是 ...

  8. topcoder srm 300 div1

    problem1 link 直接模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; public ...

  9. topcoder srm 315 div1

    problem1  link 直接模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; publi ...

  10. topcoder srm 687 div1

    1.$A_{1}=2,A_{2}=3,A_{n}=A_{n-2}+A_{n-1}-1$.给出数字$n$,将其表示成若干个$A$中的不同元素的和. 思路:设$B_{n}=A_{n}-1$,那么有$B_{ ...