查询数据的学习与练习

建立一个表

 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. Prometheus监控学习笔记之Prometheus监控简介

    0x00 Prometheus容器监控解决方案 Prometheus(普罗米修斯)是一个开源系统监控和警报工具,最初是在SoundCloud建立的.它是一个独立的开放源码项目,并且独立于任何公司.不同 ...

  2. Golang操作结构体、Map转化为JSON

    结构体生成Json package main import ( "encoding/json" "fmt" ) type IT struct { Company ...

  3. rsync: read error: Connection reset by peer (104)

    Centos7    rsync守护进程上传文件失败 [root@nfs ~]# rsync -avz /etc rsync_backup@172.16.1.41::backupsending inc ...

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

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

  5. topcoder srm 430 div1

    problem1 link 其实就是找到一个数字$t$,使得$x$的二进制为1 的位上$t$也都为1.然后$t$删掉所有那些$x$为1的二进制位就是$k$. problem2 link 设所有合法的边 ...

  6. openwrt编译e2fsprogs-1.43时报错misc/create_inode.c:399:18: error: conflicting types for 'copy_file_range'

    1. 详细报错信息 misc/create_inode.c:399:18: error: conflicting types for 'copy_file_range' static errcode_ ...

  7. 大臣的旅费|2013年蓝桥杯A组题解析第十题-fishers

    标题:大臣的旅费 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市 ...

  8. Oracle用户被锁定解决方法

    解决方法: 1.用dba角色登陆:2.输入下面格式命令解锁: alter user 用户名 account unlock;3.如果密码忘记了,输入下面格式命令修改密码: alter user 用户名 ...

  9. P2633 Count on a tree

    思路 运用树上差分的思想,转化成一个普通的主席树模型即可求解 代码 #include <cstdio> #include <algorithm> #include <cs ...

  10. 如何创建并运行java线程 , 多线程使用

    http://www.importnew.com/20672.html https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblo ...