1:创建函数的语句
create function py_trim(str varchar(100)) returns varchar(100)
begin
declare x varchar(100);
set x=ltrim(rtrim(str));
return x;
end
2:查看:
- 所有函数存储在mysql数据库下的proc表中
- 说明:存储过程与函数都存储在proc表中,区别在type字段,func表中无数据
- 查看python数据库中的函数
-
select name,type from mysql.proc where db='python';
3:调用:
select 函数名称(参数列表);
4:删除
drop function 函数名称;
5:变量
(1)声明变量,语法如下
declare 变量名 类型 default 默认值;
例:
declare x varchar(100);
(2)设置变量值,语法如下
set 变量名=值;
例:
set x='abc';
(3)使用变量:将变量写到表达式中,即可调用变量的值
6:判断语句
if 条件1 then
语句1;
elseif 条件2 then
语句2;
else
语句
end if;
7:循环
while 条件 do
语句;
end while
# 退出循环:leave,相当于break
# 退出本次循环:iterate,相当于continue
8:示例:
(1):要求:定义函数odd,输出1-100间的偶数
create function odd () returns varchar(300)
begin
declare i int default 1;
declare x varchar(300) default '';
while i<=100 do
if i%2=0 then
set x=concat(x,' ',i);
end if;
set i=i+1;
end while;
return x;
end
用法:输出1-100间的偶数:
select odd();
(2):要求:定义函数tdd,接受一个整数参数如果该数是偶数,返回该数,否则返回0;
create function tdd (x int) returns varchar(300)
begin
if x%2=0 then
return x;
else
return 0;
end if;
end
用法:输出id是偶数的行:
select * from student where id=tdd(id);
9:字符串的方法
(1)concat(str1,str2,…):参数连成一个长字符串并返回(任何参数是null时返回null)
mysql> select concat(‘my’, ‘s’, ‘ql’);
-> ‘mysql’
mysql> select concat(‘my’, null, ‘ql’);
-> null
(2)length:返回字符串str的长度(对于多字节字符char_length仅计算一次)
mysql> select length(‘text’);
-> 4
(3)locate(substr,str,pos):返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)
mysql> select locate(‘bar’, ‘foobarbar’,5);
-> 7
(4)instr(str,substr):返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)
mysql> select instr(‘foobarbar’, ‘bar’);
-> 4
(5)lpad(str,len,padstr):用字符串padstr填补str左端直到字串长度为len并返回
mysql> select lpad(‘hi’,4,’??’);
-> ‘??hi’
(6)rpad(str,len,padstr):用字符串padstr填补str右端直到字串长度为len并返回
mysql> select rpad(‘hi’,5,’?’);
-> ‘hi???’
(7)left(str,len):用字符串padstr填补str右端直到字串长度为len并返回
mysql> select rpad(‘hi’,5,’?’);
-> ‘hi???’
(8)right(str,len):返回字符串str的右端len个字符
mysql> select right(‘foobarbar’, 4);
-> ‘rbar’
(9) substring(str,pos,len):返回字符串str的位置pos起len个字符
mysql> select substring(‘quadratically’,5,6);
-> ‘ratica’
(10)substring(str,pos)/substring(str from pos):返回字符串str的位置pos起的一个子串
mysql> select substring(‘quadratically’,5);
-> ‘ratically’
mysql> select substring(‘foobarbar’ from 4);
-> ‘barbar’
(11)substring_index(str,delim,count):返回从字符串str的第count个出现的分隔符delim之后的子串(count为正数时返回左端,否则返回右端子串)
mysql> select substring_index(‘www.mysql.com’, ‘.’, 2);
-> ‘www.mysql’
mysql> select substring_index(‘www.mysql.com’, ‘.’, -2);
-> ‘mysql.com’
(12)replace(str,from_str,to_str):用字符串to_str替换字符串str中的子串from_str并返回
mysql> select replace(‘www.mysql.com’, ‘w’, ‘ww’);
-> ‘wwwwww.mysql.com’
(13) lcase(str)/lower(str):返回小写的字符串str
mysql> select lcase(‘quadratically’);
-> ‘quadratically’
(14)ucase(str)/upper(str):返回大写的字符串str
mysql> select ucase(‘quadratically’);
-> ‘quadratically’
(15)load_file(file_name):读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
不完整,没有权限,长度大于max_allowed_packet会返回null)
mysql> update table_name set blob_column=load_file
("/tmp/picture") where id=1;
10:解决的bug
(1)报错 :drop table if exists T Error Code: 1046. No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.
错误原因:未选定默认的数据库
解决方法:双击SCHEMAS列表中的某个数据库便可。