《MySQL必知必会》笔记1——检索搜索数据
1 SQL
数据库(database) :保存有组织的数据的容器
表(table): 某种特定类型数据的结构化清单,数据库中的每个表都有一个名字,用来标识自己。此名字是唯一的,这表示数据库中没有其他表具有相同的名字。表由列组成。列中存储着表中某部分的信息,如果将表想象为网格,网格中垂直的列为表列,水平行为表行。
主键(primary key):一列(或一组列),其值能够唯一区分表中每个行。
表中的任何列都可以作为主键,只要它满足以下条件:
❑ 任意两行都不具有相同的主键值;
❑ 每个行都必须具有一个主键值(主键列不允许NULL值)。
2 MySQL
2.1连接
为了连接到MySQL,需要以下信息:
❑ 主机名(计算机名)——如果连接到本地MySQL服务器,为localhost;
❑ 端口(如果使用默认端口3306之外的端口);
❑ 一个合法的用户名;
❑ 用户口令(如果需要)。
2.2选择数据库
为了使用crashcourse数据库:
use crashcourse;
2.3显示数据库、表、列
- 显示数据库
show databases;
- 显示表
show tables;
- 显示列
show columns from customers;
3 MySQL语句——检索数据
select语句-检索数据
1.检索单个列
select prod_name
from products;
**注意:**SQL语句和大小写 请注意,SQL语句不区分大小写,因此SELECT与select是相同的
2. 检索多个列
select prod_id,prod_name,prod_price
from product;
用逗号格开
3. 检索所有列
select *
from products;
4.检索不同的行
select distinct vend_id
from products;
SELECT DISTINCT vend_id告诉MySQL只返回不同(唯一)的vend_id行
5.限制结果
select prod_name
from products
limit 5;
LIMIT 5指示MySQL返回不多于5行
select prod_name
from products
limit 5,5;
LIMIT 5, 5指示MySQL返回从行5开始的5行。
6.使用完全限定的表名
select products.prod_name
from crashcourse.products;
order by-排序数据
1.排列数据
select prod_name
from products
order by prod_name;
对prod_name列以字母顺序排序数据
2.按多个列排序
select prod_id,prod_price,prod_name
from products
order by prod_price,prod_name;
输出:
仅在多个行具有相同的prod_price值时才对产品按prod_name进行排序
3.指定排序方向
select prod_id,prod_price,prod_name
from products
order by prod_price DESC
DESC关键字:以降序(从Z到A)顺序排序
4 MySQL语句——搜索数据
where语句-过滤数据
1.使用WHERE子句
select prod_name,prod_price
from products
where prod_price = 2.50;
如果在客户机上过滤数据,服务器不得不通过网络发送多余的数据,这将导致网络带宽的浪费。一般使用数据库处理数据,然后再发往应用层
2.WHERE子句操作符
select vend_id,prod_name
from products
where vend_id<>1003;
不检索vend_id = 1003行
select prod_name,prod_price
from products
where prod_price between 5 and 10;
检索prod_price在5-10
select cust_id
from custoners
where cust_email is null;
检索cust_email为Null
3.组合WHERE子句
AND操作符
select prod_id,prod_price,prod_name
from products
where vend_id = 1003 and prod_price <= 10;
OR操作符
select prod_id,prod_price,prod_name
from products
where vend_id = 1003 and prod_price <= 10;
计算次序
假如需要列出价格为10美元(含)以上且由1002或1003制造的所有产品。
select prod_name,prod_price
from products
where (vend_id = 1002 or vend_id = 1003) and prod_price > = 10;
SQL(像多数语言一样)在处理OR操作符前,优先处理AND操作符,需要在or语句加括号
IN操作符
IN操作符用来指定条件范围,范围中的每个条件都可以进行匹配。IN取合法值的由逗号分隔的清单,全都括在圆括号中。
select prod_name,prod_price
from products
where vend_id in (1002,1003)
NOT操作符
NOT WHERE子句中用来否定后跟条件的关键字
select prod_name,prod_price
from products
where vend_id not in (1002,1003)
通配符-数据过滤
通配符(wildcard) 用来匹配值的一部分的特殊字符
1.LIKE操作符
*百分号(%)通配符
在搜索串中,%表示任何字符出现任意次数
select prod_id,prod_name
from products
where prod_name like 'jet%';
下划线(_)通配符
下划线的用途与%一样,但下划线只匹配单个字符而不是多个字符。
select prod_id,prod_name
from products
where prod_name like '_ton anvil';
比较与"%"区别
select prod_id,prod_name
from products
where prod_name like '%ton anvil';
显示匹配搜索模式的行:第一行中下划线匹配1,第二行中匹配2。.5 tonanvil产品没有匹配,因为搜索模式要求匹配两个通配符而不是一个。
与%能匹配0个字符不一样,_总是匹配一个字符,不能多也不能少。
正则表达式进行搜索
基本字符匹配
prod_name包含文本1000的所有行:
select prod_name
from products
where prod_name REGEXP '1000'
order by prod_name;
进行OR匹配
select prod_name
from products
where prod_name REGEXP '1000|2000';
匹配几个字符之一
匹配任何单一字符。但是,如果你只想匹配特定的字符,怎么办?可通过指定一组用[和]括起来的字符来完成,如下所示:
select prod_name
from products
where prod_name REGEXP '[123] Ton';
[123]定义一组字符,它的意思是匹配1或2或3
匹配范围
select prod_name
from products
where prod_name REGEXP '[1-5] Ton';
由于5 ton匹配,所以返回.5 ton
匹配特殊字符
为了匹配特殊字符,必须用\为前导。\-表示查找-, \.表示查找.
select vend_name
from vendors
where vend_name REGEXP '\\.';
匹配字符类
select prod_name
from products
where prod_name REGEXP '\\([0-9] sticks?\\)';
sticks?匹配stick和sticks(s后的?使s可选,因为?匹配它前面的任何字符的0次或1次出现)
select prod_name
from products
where prod_name REGEXP '[[:digit:]]{4}'
select prod_name
from products
where prod_name REGEXP '^[0-9\\.]';