示例库:
DROP TABLE IF EXISTS `tb_book`;
CREATE TABLE `tb_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`books` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`category` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`user` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sale` int(11) DEFAULT '0',
`sort` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of tb_bookca
-- ----------------------------
INSERT INTO `tb_book` VALUES ('1', 'PHP开发典型模块大全', 'PHP', 'mr', '12', '模块类');
INSERT INTO `tb_book` VALUES ('2', 'Java项目开发全程实录', 'Java', 'mrsoft', '95', '项目类');
INSERT INTO `tb_book` VALUES ('27', 'Java Web从入门到精通', 'Java Web', 'lx', '4', '基础类');
INSERT INTO `tb_book` VALUES ('41', 'Java范例完全自学手册', 'Java', 'mr', '0', '范例类');
运算符
算数运算符
除了DIV与MOD只有两个参数外,其余可以同时运算多个操作数,在取余和除法运算时,如果x2参数是0,则结果为空(NULL)
算数运算符示例:
select id,books,sale,sale+sale,sale*sale,sale/sale from tb_book;
比较运算符:最常用的一类运算
注意:
在运用运算符“=”时是通过ASCII码进行判断的;
“=、<>、!=、>、<、>=、<=”都不能用来判断空值。否则将返回NULL;
NULL与'NULL'是不同的,前者表示为控制,后者为字符串
比较运算符示例:
select id,books,id=1 from tb_book;
select id,books,id>=1 from tb_book;
select id,books,id between 1 and 15 from tb_book;
select id,books,books like '%java%' from tb_book;
select id,books,books regexp '^P',user regexp 'r$' from tb_book;
逻辑运算符
注释:异或:两个结果都为真或都不为真则结果为假,如果一个为真一个为假则结果为真
逻辑运算符示例:(在数据库中非零为真否则为假)
select id,books,sale,sale && 0,sale && 1 ,sale && 100 from tb_book;
select id,books,sale,sale || 0,sale || 1 ,sale || 100 from tb_book;
select id,books,sale,sale xor 0,sale xor 1 ,sale xor 100 from tb_book;
位运算符
位运算符示例:
select 4&6,4|6,~4;
运算符的优先级
流程控制语句
IF语句:
Usage:
IF condition THEN
…
[ELSE condition THEN]
…
[ELSE]
…
ENDIF
示例:
delimiter // #设置结束符为//。默认结束符为;,因为语句中会用到;,所以一般需要重新设定结束符
create procedure example_if(in x int) #创建一个命名为example_if的存储过程,并接收一个 int 类型的位置参数x
begin #表示开始编写SQL语句
if x = 1 then
select 1;
elseif x = 2 then
select 2;
else
select 3;
end if;
end #结束SQL语句
//
call example_if(5)// #调用此存储过程进行验证
CASE语句:
Usage:
CASE value
WHEN value THEN …
[WHEN valueTHEN…]
[ELSE…]
END CASE
示例:
create procedure example_case(in x int)
begin
case x
when 1 then select 1;
when 2 then select 2;
else select 3;
end case;
end
//
call example_case(5)// #调用此存储过程进行验证
WHILE循环语句
Usage:
while condition do
…
end while;
示例:
delimiter //
create procedure example_while(out sum int)
begin
declare i int default 1;
declare s int default 0;
while i <=100 do
set s = s+i;
set i = i+1;
end while;
set sum = s;
end
//
call example_while(@s)// #调用
select @s // #显示
LOOP循环语句 需要给定停止执行判断,否则一直执行
Usage:
loop
…
end loop
示例:
delimiter //
create procedure example_loop3(out sum int)
begin
declare i int default 1;
declare s int default 0;
loop_label: loop #开始循环loop_label循环体
set s = s+i;
set i = i+1;
if i > 100 then
leave loop_label; #结束循环体loop_label
end if ;
end loop;
set sum = s;
end
//
call example_while(@s)// #调用
select @s // #显示
REPEAT循环语句:先执行一次循环,执行为真则退出,否则继续执行
Usage:
REPEAT
…
UNTIL condition
END REPEAT
示例:
delimiter //
create procedure example_repeat(out sum int)
begin
declare i int default 1;
declare s int default 0;
repeat
set s = s+i;
set i = i+1;
until i > 100
end repeat;
set sum = s;
end
//
call example_while(@s)// #调用
select @s // #显示