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'

原文地址:https://www.cnblogs.com/Manual-Linux/p/10181922.html