文章目录
环境准备:
use gcc;
create table location (Region char(20),Store_Name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');
select * from location;
location 表格如下:
+--------+-------------+
| Region | Store_Name |
+--------+-------------+
| East | Boston |
| East | New York |
| West | Los Angeles |
| West | Houston |
+--------+-------------+
4 rows in set (0.00 sec)
create table Store_Info (Store_Name char(20),Sales int(10),Date char(10));
insert into Store_Info values('Los Angeles','1500','2020-12-05');
insert into Store_Info values('Houston','250','2020-12-07');
insert into Store_Info values('Los Angeles','300','2020-12-08');
insert into Store_Info values('Boston','700','2020-12-08');
select * from Store_Info;
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Houston | 250 | 2020-12-07 |
| Los Angeles | 300 | 2020-12-08 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
4 rows in set (0.00 sec)
-----SELECT -------显示表格中一个或数个栏位的所有资料
语法: SELECT "栏位" FROM "表名";
SELECT Store_Name FROM Store_Info;
---- DISTINCT ----不显示重复的资料
语法: SELECT DISTINCT "栏位" FROM "表名";
SELECT DISTINCT Store_Name FROM Store_Info;
-----WHERE-------有条件查询
语法: SELECT "栏位" FROM "表名" WHERE "条件";
SELECT Store_Name FROM Store_Info WHERE Sales > 1000;
-----AND OR -----且 或
语法: SELECT "栏位" FROM "表名" WHERE "条件1" ([AND|OR] "条件2")+ ;
SELECT Store_Name FROM Store_Info WHERE Sales > 1000 OR (Sales < 500 AND Sales > 200);
----IN-----------显示已知的值的资料
语法: SELECT "栏位" FROM "表名" WHERE "栏位" IN ('值1','值2',...);
SELECT * FROM Store_Info WHERE Store_Name IN ('Los Angeles','Houston');
----BETWEEN------显示两个值范围内的资料
语法: SELECT "栏位" FROM "表名" WHERE "栏位" BETWEEN '值1' AND '值2';
SELECT * FROM Store_Info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';
------通配符-------通常通配符都是跟 LIKE 一起使用的
% : 百分号表示零个、一个或多个字符
_ : 下划线表示单个字符
'A_Z': 所有以'A'起头,另一个任何值的字符,且以'Z'为结尾的字符串。例如,'ABZ' 和'A2Z'都符合这一个模式,而'AKKZ' 并不符合
'ABC%': 所有以'ABC' 开头的字符串。 例如,'ABCD' 和'ABCABC' 都符合这个模式。
'%XYZ': 所有以'XYZ' 结尾的字符串。 例如,'WXYZ' 和'ZZXYZ' 都符合这个模式。
'%AN%': 所有含有'AN'这个模式的字符串。例如,'LOS ANGELES'和'SAN FRANCISCO' 都符合这个模式。
'_AN%': 所有第二个字母为'A' 和第三个字母为'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而'LOS ANGELES'则不符合这个模式。
------LIKE----匹配一个模式来找出我们要的资料
语法: SELECT "栏位” FROM "表名" WHERE "栏位" LIKE {模式};
SELECT * FROM Store_Info WHERE Store_Name like '%os%';
---- ORDER BY -----按关键字排序
语法: SELECT "栏位" FROM "表名" [WHERE "条件"] ORDER BY "栏位" [ASC,DESC];
#ASC是按照升序进行排序的,是默认的排序方式。
#DESC是按降序方式进行排序。
SELECT Store_Name,Sales,Date FROM Store_Info ORDER BY Sales DESC;
------------------------函数-------------------------
1、数学函数:
函数 | 作用 |
abs(x) | 返回 x 的绝对值 |
rand() | 返回 0 到 1 的随机数 |
mod(x,y) | 返回 x 除以 y 以后的余数 |
power(x,y) | 返回 x 的 y 次方 |
round(x) | 返回离 x 最近的整数 |
round(x,y) | 保留 x 的 y 位小数四舍五入后的值 |
sqrt(x) | 返回 x 的平方根 |
truncate(x,y) | 返回数字 x 截断为 y 位小数的值 |
ceil(x) | 返回大于或等于 x 的最小整数 |
floor(x) | 返回小于或等于 x 的最大整数 |
greatest(x1,x2…) | 返回集合中最大的值 |
least(x1,x2…) | 返回集合中最小的值 |
举例:
SELECT abs(-1),rand(),mod(5,3),power(2,3),round(1.89);
SELECT round(1.8937,3),truncate(1.253,2),ceil(5.2),floor(2.1),least(1.89,3,6.1,3.3);
2、聚合函数:
函数 | 作用 |
avg() | 返回指定列的平均值 |
count() | 返回指定列中非NULL值的个数 |
min() | 返回指定列的最小值 |
max() | 返回指定列的最大值 |
sum(x) | 返回指定列的所有值之和 |
举例:
SELECT avg(Sales) FROM Store_Info;
SELECT count(Store_Name) FROM Store_Info;
SELECT count(DISTINCT Store_Name) FROM Store_Info; #DISTINCT为不显示重复信息
#count(*) 包括了所有的列的行数,在统计结果的时候,不会忽略列值为NULL(空)的行;
#count(列名) 只包括列名那一行的行数,在统计结果的时候,会忽略列值为NULL(空)的行。
SELECT max(Sales) FROM Store_Info;
SELECT min(Sales) FROM Store_Info;
SELECT sum(Sales) FROM Store_Info;
3、字符串函数:
函数 | 作用 |
trim() | 返回去除指定格式的值 |
concat(x,y) | 将提供的参数x和y拼接成一个字符串 |
substr(x,y) | 获取从字符串x中的第y个位置开始的字符串,跟substring() 函数作用相同 |
substr(x,y,z) | 获取从字符串x中的第y个位置开始长度为z的字符串 |
length(x) | 返回字符串x的长度 |
replace(x,y,z) | 用字符串z替代字符串x中的字符串y |
upper(x) | 将字符串x的所有字母变成大写字母 |
lower(x) | 将字符串x的所有字母变成小写字母 |
left(x,y) | 返回字符串x的前y个字符 |
right(x,y) | 返回字符串x的后y个字符 |
repeat(x,y) | 将字符串x重复y次 |
space(x) | 返回x个空格 |
strcmp(x,y) | 比较x和y,返回的值可以为-1,0,1 |
reverse(x) | 将字符串x反转 |
举例:
SELECT concat(Region,Store_Name) FROM location WHERE Store_Name ='Boston';
结果如下:
+---------------------------+
| concat(Region,Store_Name) |
+---------------------------+
| EastBoston |
+---------------------------+
1 row in set (0.00 sec)
#如sql_mode开启 开启了PIPES_AS_CONCAT(该模块在/etc/my.cnf中查看是否开启),"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样
SELECT Region || ' ' || Store_Name FROM location WHERE Store_Name = 'Boston';
结果如下:
+-----------------------------+
| Region || ' ' || Store_Name |
+-----------------------------+
| East Boston |
+-----------------------------+
1 row in set (0.00 sec)
SELECT substr(Store_Name,3) FROM location WHERE Store_Name = 'Los Angeles';
SELECT substr(Store_Name,2,4) FROM location WHERE Store_Name = 'New York';
SELECT TRIM ([位置] [要移除的字符串] FROM 字符串);
#[位置]: 值可以为 LEADING (起头),TRAILING (结尾),BOTH (起头及结尾)。
#[要移除的字符串]: 从字串的起头、结尾或起头及结尾移除的字符串。缺省时为空格。
SELECT TRIM(LEADING 'Ne' FROM 'New York');
SELECT Region,length(Store_Name) FROM location;
mysql> select Region,length(Store_name) from location;
+--------+--------------------+
| Region | length(Store_name) |
+--------+--------------------+
| East | 6 |
| East | 8 |
| West | 11 |
| West | 7 |
+--------+--------------------+
4 rows in set (0.00 sec)
SELECT REPLACE (Region,'ast','astern') FROM location;
------GROUP BY------对GROUP BY后面的栏位的查询结果进行汇总分组,通常是结合聚合函数一起使用的
GROUP BY 有一个原则,就是 SELECT 后面的所有列中,没有使用聚合函数的列,必须出现在GROUPBY后面。
语法: SELECT "栏位1",SUM("栏位2") FROM "表名" GROUP BY "栏位1";
SELECT Store_Name,SUM(Sales) FROM Store_Info GROUP BY Store_Name ORDER BY sales desc;
mysql> select Store_Name,sum(Sales) from Store_Info group by Store_Name order by sales desc;
+-------------+------------+
| Store_Name | sum(Sales) |
+-------------+------------+
| Houston | 250 |
| Washington | 300 |
| Boston | 700 |
| Los Angeles | 1500 |
+-------------+------------+
4 rows in set (0.00 sec)
------- HAVING ----用来过滤由 GROUP BY 语句返回的记录集,通常与 GROUP BY 语句联合使用
HAVING 语句的存在弥补了 WHERE 关键字不能与聚合函数联合使用的不足。如果被 SELECT 的只有函数栏,那就不需要GROUP BY子句。
语法: SELECT "栏位1",SUM("栏位2") FROM "表名" GROUP BY "栏位1" HAVING (函数条件);
SELECT Store_Name,SUM(Sales) FROM Store_Info GROUP BY Store_Name HAVING SUM(Sales) > 1500;
---- ----别名---------栏位別名 表格别名
语法: SELECT "表格别名"."栏位1" [AS] "栏位别名" FROM "表格名" [AS] "表格别名";
SELECT A.Store_Name Store,SUM(A.Sales) "Total Sales" FROM Store_Info A GROUP BY A.Store_Name;
---------子查询--------连接表格,在WHERE子句或HAVING 子句中插入另一个SQL语句
语法: SELECT "栏位1" FROM "表格1" WHERE "栏位2” [比较运算符] #外查询
(SELECT "栏位1" FROM "表格2" WHERE "条件"); #内查询
#可以是符号的运算符,例如=、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
SELECT SUM(Sales) FROM Store_Info WHERE Store_Name IN
(SELECT Store_Name FROM location WHERE Region = 'West');
SELECT SUM(A.Sales) FROM Store_Info A WHERE A.Store_Name IN
(SELECT Store_Name FROM location B WHERE B.Store_Name = A.Store_Name);
SELECT Store_Name,SUM(Sales),COUNT(Sales) FROM Store_Info GROUP BY Store_Name ORDER BY Sales;
+-------------+------------+--------------+
| Store_Name | SUM(Sales) | COUNT(Sales) |
+-------------+------------+--------------+
| Houston | 250 | 1 |
| Boston | 700 | 1 |
| Los Angeles | 1800 | 2 |
+-------------+------------+--------------+
3 rows in set (0.01 sec)
------- EXISTS ------用来测试内查询有没有产生任何结果,类似布尔值是否为真
#如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个SQL语句就不会产生任何结果。
语法: SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
SELECT SUM(Sales) FROM Store_Info WHERE EXISTS (SELECT * FROM location WHERE Region = 'West');
SELECT SUM(Sales) FROM Store_Info WHERE Store_Name IN ('Los Angeles','Houston');
+------------+
| SUM(Sales) |
+------------+
| 2050 |
+------------+
1 row in set (0.00 sec)
--------------连接查询----------------
location 表格如下:
+--------+-------------+
| Region | Store_Name |
+--------+-------------+
| East | Boston |
| East | New York |
| West | Los Angeles |
| West | Houston |
+--------+-------------+
4 rows in set (0.00 sec)
UPDATE Store_Info SET store_name='Washington' WHERE sales=300;
Store_Info 表格如下:
mysql> select * from Store_Info;
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Houston | 250 | 2020-12-07 |
| Washington | 300 | 2020-12-08 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
4 rows in set (0.00 sec)
inner join(等值相连): 只返回两个表中联结字段相等的行
left join(左联接): 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接): 返回包括右表中的所有记录和左表中联结字段相等的记录
SELECT * FROM location A INNER JOIN Store_Info B on A.Store_Name = B.Store_Name;
mysql> SELECT * FROM location A INNER JOIN Store_Info B on A.Store_Name = B.Store_Name;
+--------+-------------+-------------+-------+------------+
| Region | Store_Name | Store_Name | Sales | Date |
+--------+-------------+-------------+-------+------------+
| West | Los Angeles | Los Angeles | 1500 | 2020-12-05 |
| West | Houston | Houston | 250 | 2020-12-07 |
| East | Boston | Boston | 700 | 2020-12-08 |
+--------+-------------+-------------+-------+------------+
3 rows in set (0.00 sec)
SELECT * FROM location A RIGHT JOIN Store_Info B on A.Store_Name = B.Store_Name;
SELECT * FROM location A,Store_Info B WHERE A.Store_Name = B.Store_Name;
SELECT A.Region REGION,SUM(B.Sales) SALES FROM location A,Store_Info B WHERE A.Store_Name = B.Store_Name GROUP BY REGION;
-------------CREATE VIEW---------视图,可以被当作是虚拟表或存储查询
视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写的SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
语法: CREATE VIEW "视图表名" AS "SELECT 语句";
CREATE VIEW V_REGION_SALES AS SELECT A.Region REGION,SUM(B.Sales) SALES FROM location A INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name GROUP BY REGION;
SELECT * FROM V_REGION_SALES;
DROP VIEW V_REGION_SALES; #删除视图
------------UNION-------联集,将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类
UNION: 生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法: [SELECT 语句1] UNION [SELECT 语句2];
UNION ALL: 将生成结果的资料值都列出来,无论有无重复
语法: [SELECT 语句1] UNION ALL [SELECT 语句2];
SELECT Store_Name FROM location UNION SELECT Store_Name FROM Store_Info;
SELECT Store_Name FROM location UNION ALL SELECT Store_Name FROM Store_Info;
-------------交集值------- 取两个SQL语句结果的交集
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name;
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B USING(Store_Name);
SELECT A.Store_Name FROM
(SELECT Store_Name FROM location UNION ALL SELECT Store_Name FROM Store_Info) A
GROUP BY A.Store_Name HAVING COUNT(*) > 1;
#取两个SQL语句结果的交集,且没有重复
SELECT A.Store_Name FROM (SELECT A.Store_Name FROM location A INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name) A
GROUP BY A.Store_Name HAVING COUNT(*) >= 1;
SELECT DISTINCT A.Store_Name FROM location A INNER JOIN Store_Info B USING(Store_Name); _Name);
--------------无交集值-------显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复
SELECT DISTINCT Store_Name FROM location WHERE (Store_Name) NOT IN (SELECT Store_Name FROM Store_Info);
SELECT DISTINCT A.Store_Name FROM location A
LEFT JOIN Store_Info B USING(Store_Name) WHERE B.Store_Name IS NULL;
----------- CASE --------是SQL用来做为 IF-THEN-ELSE 之类逻辑的关键字
语法:
SELECT CASE ("栏位名")
WHEN "条件1" THEN "结果1"
WHEN "条件2" THEN "结果2"
......
[ELSE "结果N"]
END
FROM "表名";
#"条件"可以是一个数值或是公式。ELSE 子句则并不是必须的。
SELECT Store_Name,CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'Boston' THEN Sales * 1.5
ELSE Sales
END
"New Sales", Date
FROM Store_Info;
#"New sales" 是用于 CASE 那个栏位的栏位名
#创建一个新表:
CREATE TABLE Total_Sales (Name char(10),Sales int(5));
INSERT INTO Total_Sales VALUES ('zhangsan',10);
INSERT INTO Total_Sales VALUES ('lisi',15);
INSERT INTO Total_Sales VALUES ('wangwu',20);
INSERT INTO Total_Sales VALUES ('zhaoliu',40);
INSERT INTO Total_Sales VALUES ('sunqi',50);
INSERT INTO Total_Sales VALUES ('zhouba',20);
INSERT INTO Total_Sales VALUES ('wujiu',30);
Total_Sales 表格如下:
mysql> select * from Total_Sales;
+----------+-------+
| Name | Sales |
+----------+-------+
| zhangsan | 10 |
| lisi | 15 |
| wangwu | 20 |
| zhaoliu | 40 |
| sunqi | 50 |
| zhouba | 20 |
| wujiu | 30 |
+----------+-------+
7 rows in set (0.00 sec)
-------算排名 ------表格自我连结 (self Join),然后将结果依序列出,算出每一行之前(包含那一行本身)有多少行数
SELECT A1.Name,A1.Sales,COUNT(A2.Sales) Rank FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
#统计sales栏位的值是比自已本身的值小的以及sales栏位和Name栏位都相同的数量,比如zhangsan为5+1=6
+----------+-------+------+
| Name | Sales | Rank |
+----------+-------+------+
| sunqi | 50 | 1 |
| zhaoliu | 40 | 2 |
| wujiu | 30 | 3 |
| wangwu | 20 | 4 |
| zhouba | 20 | 4 |
| lisi | 15 | 6 |
| zhangsan | 10 | 7 |
+----------+-------+------+
7 rows in set (0.00 sec)
----------算中位数 ------------
SELECT Sales Middle FROM (SELECT A1.Name,A1.Sales,COUNT(A2.Sales) Rank FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name <= A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC) A3
WHERE A3.Rank = (SELECT (COUNT(*)+1) DIV 2 FROM Total_Sales);
#每个派生表必须有自己的别名,所以别名 A3 必须要有
#DIV 是在MySQL中算出商的方式
+--------+
| Middle |
+--------+
| 20 |
+--------+
1 row in set (0.00 sec)
-------- 算累积总计-------表格自我连结(Self Join), 然后将结果依序列出,算出每一行之前(包含那一行本身)的总合
SELECT A1.Name,A1.Sales,SUM(A2.Sales) Sum_Total FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
+----------+-------+-----------+
| Name | Sales | Sum_Total |
+----------+-------+-----------+
| sunqi | 50 | 50 |
| zhaoliu | 40 | 90 |
| wujiu | 30 | 120 |
| zhouba | 20 | 140 |
| wangwu | 20 | 140 |
| lisi | 15 | 175 |
| zhangsan | 10 | 185 |
+----------+-------+-----------+
7 rows in set (0.00 sec)
---------算总合百分比--------------
SELECT A1.Name,A1.Sales,A1.Sales/(SELECT SUM(Sales) FROM Total_Sales) Per_Total
FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
#SELECT SUM(Sales) FROM Total_sales 这一段子查询是用来算出总合
#总合算出后,我们就能够将每一行一一除以总合来求出每一行的总合百分比
+----------+-------+-----------+
| Name | Sales | Per_Total |
+----------+-------+-----------+
| sunqi | 50 | 0.2703 |
| zhaoliu | 40 | 0.2162 |
| wujiu | 30 | 0.1622 |
| zhouba | 20 | 0.1081 |
| wangwu | 20 | 0.1081 |
| lisi | 15 | 0.0811 |
| zhangsan | 10 | 0.0541 |
+----------+-------+-----------+
7 rows in set (0.00 sec)
--------算累积总合百分比--------------
SELECT A1.Name,A1.Sales,SUM(A2.Sales)/(SELECT SUM(Sales) FROM Total_Sales) Per_Total
FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales and A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
+----------+-------+-----------+
| Name | Sales | Per_Total |
+----------+-------+-----------+
| sunqi | 50 | 0.2703 |
| zhaoliu | 40 | 0.4865 |
| wujiu | 30 | 0.6486 |
| zhouba | 20 | 0.7568 |
| wangwu | 20 | 0.7568 |
| lisi | 15 | 0.9459 |
| zhangsan | 10 | 1.0000 |
+----------+-------+-----------+
7 rows in set (0.00 sec)
#用累积总计SUM(a2.sales) 除以总合来求出每一行的累积总合百分比.
SELECT A1.Name,A1.sales,TRUNCATE(ROUND(SUM(A2.Sales)/(SELECT SUM(Sales) FROM Total_Sales),4)*100,2) || '%' Per_Total
FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales and A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
+----------+-------+-----------+
| Name | sales | Per_Total |
+----------+-------+-----------+
| sunqi | 50 | 27.03% |
| zhaoliu | 40 | 48.65% |
| wujiu | 30 | 64.86% |
| zhouba | 20 | 75.68% |
| wangwu | 20 | 75.68% |
| lisi | 15 | 94.59% |
| zhangsan | 10 | 100.00% |
+----------+-------+-----------+
7 rows in set (0.00 sec)
--------- 空值(NULL) 和无值('')的区别---------
1.无值的长度为0,不占用空间的; 而NULL值的长度是NULL,是占用空间的。
2. Is NULL或者Is NOT NULL,是用来判断字段是不是为NULL或者不是NULL,不能查出是不是无值的。
3.无值的判断使用 ='' 或者 <>'' 来处理。 <>代表不等于。
4.在通过 count()指定字段统计有多少行数时,如果遇到NULL 值会自动忽略掉,遇到无值会加入到记录中进行计算。
创建City表格:
use gcc;
create table city (name char(10));
insert into city values('beijing');
insert into city values('nanjing');
insert into city values('shanghai');
insert into city values();
insert into city values();
insert into city values();
insert into city values('');
insert into city values('');
mysql> select * from city;
+----------+
| name |
+----------+
| beijing |
| nanjing |
| shanghai |
| NULL |
| NULL |
| NULL |
| |
| |
+----------+
SELECT length (NULL),length(''),length('1');
+---------------+------------+-------------+
| length (NULL) | length('') | length('1') |
+---------------+------------+-------------+
| NULL | 0 | 1 |
+---------------+------------+-------------+
1 row in set (0.00 sec)
SELECT * FROM city WHERE name IS NULL; #不会把无值计算进去
SELECT * FROM city WHERE name IS NOT NULL; #会把无值也计算进去
SELECT * FROM city WHERE name = '';
SELECT * FROM city WHERE name <> '';
SELECT COUNT(*) FROM city; #空值和无值都会计算进去
SELECT COUNT(name) FROM city; #忽略空值,但是会把无值计算进去
-------------正则表达式------------------
匹配模式 描述 实例
^ 匹配文本的开始字符 ‘^bd’ 匹配以 bd 开头的字符串
$ 匹配文本的结束字符 ‘qn$’ 匹配以 qn 结尾的字符串
. 匹配任何单个字符 ‘s.t’ 匹配任何 s 和 t 之间有一个字符的字符串
* 匹配零个或多个在它前面的字符 ‘fo*t’ 匹配 t 前面有任意个 o
+ 匹配前面的字符 1 次或多次 ‘hom+’ 匹配以 ho 开头,后面至少一个m 的字符串
字符串 匹配包含指定的字符串 ‘clo’ 匹配含有 clo 的字符串
p1|p2 匹配 p1 或 p2 ‘bg|fg’ 匹配 bg 或者 fg
[...] 匹配字符集合中的任意一个字符 ‘[abc]’ 匹配 a 或者 b 或者 c
[^...] 匹配不在括号中的任何字符 ‘[^ab]’ 匹配不包含 a 或者 b 的字符串
{n} 匹配前面的字符串 n 次 ‘g{2}’ 匹配含有 2 个 g 的字符串
{n,m} 匹配前面的字符串至少 n 次,至多m 次 ‘f{1,3}’ 匹配 f 最少 1 次,最多 3 次
语法:
SELECT "栏位” FROM "表名" WHERE "栏位" REGEXP {模式};
SELECT * FROM Store_Info WHERE Store_Name REGEXP 'os'; #匹配包含os的字符串
SELECT * FROM Store_Info WHERE Store_Name REGEXP '^[A-G]'; #匹配以A-G开头的字符串
SELECT * FROM Store_Info WHERE Store_Name REGEXP 'Ho|Bo'; #匹配具有Ho或Bo的字符串
-------------存储过程----------------
1、存储过程是一组为了完成特定功能的SQL语句集合。
2、存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来,这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时,只需要调用它即可。存储过程在执行上比传统SQL速度更快、执行效率更高。
3、存储过程的优点:
(1)执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
(2)SQL语句加上控制语句的集合,灵活性高
(3)在服务器端存储,客户端调用时,降低网络负载
(4)可多次重复被调用,可随时修改,不影响客户端调用
(5)可完成所有的数据库操作,也可控制数据库的信息访问权限
##创建存储过程##
DELIMITER $$ #将语句的结束符号从分号;临时改为两个$$(可以是自定义)
CREATE PROCEDURE Proc() #创建存储过程,过程名为Proc,不带参数
-> BEGIN #过程体以关键字 BEGIN 开始
-> select * from Store_Info; #过程体语句
-> END $$ #过程体以关键字 END 结束
DELIMITER ; #将语句的结束符号恢复为分号
##调用存储过程##
CALL Proc;
##查看存储过程##
SHOW CREATE PROCEDURE [数据库.]存储过程名; #查看某个存储过程的具体信息
SHOW CREATE PROCEDURE Proc;
SHOW PROCEDURE STATUS [LIKE '%Proc%'] \G
##存储过程的参数##
IN 输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
OUT 输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
INOUT 输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)
举例:
DELIMITER $$
CREATE PROCEDURE Proc1(IN inname CHAR(16))
-> BEGIN
-> SELECT * FROM Store_Info WHERE Store_Name = inname;
-> END $$
DELIMITER ;
CALL Proc1('Boston');
##删除存储过程##
存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名称创建新的存储过程。
DROP PROCEDURE IF EXISTS Proc;
##存储过程的控制语句##
create table t (id int(10));
insert into t values(10);
(1)条件语句if-then-else-end if
DELIMITER $$
CREATE PROCEDURE proc2(IN parameter int)
-> begin
-> declare var int;
-> set var=parameter*2;
-> if var>=10 then
-> update t set id=id+1;
-> else
-> update t set id=id-1;
-> end if;
-> end $$
DELIMITER ;
CALL Proc2(6);
(2)循环语句while ···· end while
DELIMITER $$
CREATE PROCEDURE proc3()
-> begin
-> declare var int(10);
-> set var=0;
-> while var<6 do
-> insert into t values(var);
-> set var=var+1;
-> end while;
-> end $$
DELIMITER ;
CALL Proc3;