??xml version="1.0" encoding="utf-8" standalone="yes"?>
2.参数传?/p>
?调用存储q程
?删除存储q程
?区块Q条Ӟ循环
3.循环语句
(1).while循环
?其他常用命o
mysql存储q程中要用到的运符
术q算W?/strong>
+ ?nbsp; SET var1=2+2; 4
- ?nbsp; SET var2=3-2; 1
* ?nbsp; SET var3=3*2; 6
/ ?nbsp; SET var4=10/3; 3.3333
DIV 整除 SET var5=10 DIV 3; 3
% 取模 SET var6=10%3 ; 1
比较q算W?/strong>
逻辑q算W?/strong>
?/strong>(AND)
AND |
TRUE |
FALSE |
NULL |
---|---|---|---|
TRUE |
TRUE |
FALSE |
NULL |
FALSE |
FALSE |
FALSE |
NULL |
NULL |
NULL |
NULL |
NULL |
?OR)
OR |
TRUE |
FALSE |
NULL |
---|---|---|---|
TRUE |
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
NULL |
NULL |
TRUE |
NULL |
NULL |
异或(XOR)
XOR |
TRUE |
FALSE |
NULL |
---|---|---|---|
TRUE |
FALSE |
TRUE |
NULL |
FALSE |
TRUE |
FALSE |
NULL |
NULL |
NULL |
NULL |
NULL |
位运符
| 位或
& 位与
<< 左移?br />
>> 右移?br />
~ 位非(单目q算Q按位取?
mysq存储q程中常用的函数Q字W串cd操作Q数学类Q日期时间类?/p>
一.字符串类
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字W?br /> UCASE (string2 ) //转换成大?br /> RIGHT(string2,length) //取string2最后length个字W?br /> SPACE(count) //生成count个空?nbsp;
?数学c?/span>
mysql> select round(1.56);
+———?+
| round(1.56) |
+———?+
| 2 |
+———?+
1 row in set (0.00 sec)
(2)可以讑֮数位数Q返回Q点型数据
mysql> select round(1.567,2);
+————?+
| round(1.567,2) |
+————?+
| 1.57 |
+————?+
1 row in set (0.00 sec)
SIGN (number2 ) //q回W号,正负?
SQRT(number2) //开qx
?日期旉c?/span>
--??- --??-
--数据操作
select --从数据库表中索数据行和列
insert --向数据库表添加新数据?BR>delete --从数据库表中删除数据?BR>update --更新数据库表中的数据
--数据定义
create table --创徏一个数据库?BR>drop table --从数据库中删除表
alter table --修改数据库表l构
create view --创徏一个视?BR>drop view --从数据库中删除视?BR>create index --为数据库表创Z个烦?BR>drop index --从数据库中删除烦?BR>create procedure --创徏一个存储过E?BR>drop procedure --从数据库中删除存储过E?BR>create trigger --创徏一个触发器
drop trigger --从数据库中删除触发器
create schema --向数据库d一个新模式
drop schema --从数据库中删除一个模?BR>create domain --创徏一个数据值域
alter domain --改变域定?BR>drop domain --从数据库中删除一个域
--数据控制
grant --授予用户讉K权限
deny --拒绝用户讉K
revoke --解除用户讉K权限
--事务控制
commit --l束当前事务
rollback --中止当前事务
set transaction --定义当前事务数据讉K特征
--E序化sql
declare --为查询设定游?BR>explan --为查询描q数据访问计?BR>open --索查询结果打开一个游?BR>fetch --索一行查询结?BR>close --关闭游标
prepare --为动态执行准备sql 语句
execute --动态地执行sql 语句
describe --描述准备好的查询
---局部变?BR>declare @id char(10)
--set @id = '10010001'
select @id = '10010001'
---全局变量
---必须以@@开?/P>
--IF ELSE
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print 'x > y' --打印字符?x > y'
else if @y > @z
print 'y > z'
else print 'z > y'
--CASE
use pangu
update employee
set e_wage =
case
when job_level = ??then e_wage*1.08
when job_level = ??then e_wage*1.07
when job_level = ??then e_wage*1.06
else e_wage*1.05
end
--while continue break
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
print @x --打印变量x 的?BR> while @y < 3
begin
select @c = 100*@x + @y
print @c --打印变量c 的?BR> select @y = @y + 1
end
select @x = @x + 1
select @y = 1
end
--WAITFOR--
--?{待1 时2 分零3 U后才执行SELECT 语句
waitfor delay ?1:02:03?BR>select * from employee
--?{到晚上11 炚w8 分后才执行SELECT 语句
waitfor time ?3:08:00?BR>select * from employee
***SELECT***
select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname = 'str_name'
stockname like '% find this %'
stockname like '[a-zA-Z]%' --------- ([]指定值的范围)
stockname like '[^F-M]%' --------- (^排除指定范围)
--------- 只能在用like关键字的where子句中用通配W?
or stockpath = 'stock_path'
or stocknumber < 1000
and stockindex = 24
not stock*** = 'man'
stocknumber between 20 and 100
stocknumber in(10,20,30)
order by stockid desc(asc) --------- 排序Qdesc-降序Qasc-升序
order by 1,2 --------- by列号
stockname = (select stockname from stock_information where stockid = 4)
--------- 子查?BR> --------- 除非能确保内层select只返回一个行的|
--------- 否则应在外层where子句中用一个in限定W?BR> select distinct column_name form table_name
--------- distinct指定索独有的列|不重?BR> select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
select stockname , "stocknumber" = count(*) from table_name group by stockname
--------- group by 表按行分组,指定列中有相同的?BR> having count(*) = 2 --------- having选定指定的组
select *
from table1, table2
where table1.id *= table2.id -------- 左外部连接,table1中有的而table2中没有得以null表示
table1.id =* table2.id -------- 叛_部连?
select stockname from table1
union [all] --------- union合ƈ查询l果集,all-保留重复?BR> select stockname from table2
***insert***
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (select Stockname , Stocknumber from Stock_table2)
-------- value为select语句
***update***
update table_name set Stockname = "xxx" [where Stockid = 3]
Stockname = default
Stockname = null
Stocknumber = Stockname + 4
***delete***
delete from table_name where Stockid = 3
truncate table_name --------- 删除表中所有行Q仍保持表的完整?BR> drop table table_name --------- 完全删除?/P>
***alter table*** --------- 修改数据库表l构
alter table database.owner.table_name add column_name char(2) null .....
sp_help table_name --------- 昄表已有特?BR> create table table_name (name char(20), age smallint, lname varchar(30))
insert into table_name select .........
--------- 实现删除列的ҎQ创建新表)
alter table table_name drop constraint Stockname_default
--------- 删除Stockname的defaultU束
***function(/*常用函数*/)***
----l计函数----
AVG --求^均?BR>COUNT --l计数目
MAX --求最大?BR>MIN --求最?BR>SUM --求和
--AVG
use pangu
select avg(e_wage) as dept_avgWage
from employee
group by dept_id
--MAX
--求工资最高的员工姓名
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
from employee)
--stdev()
--stdev()函数q回表达式中所有数据的标准?/P>
--stdevp()
--stdevp()函数q回M标准?/P>
--var()
--var()函数q回表达式中所有值的l计变异?/P>
--varp()
--varp()函数q回M变异?/P>
----术函数----
/***三角函数***/
sin(float_expression) --q回以弧度表C的角的正u
cos(float_expression) --q回以弧度表C的角的余u
tan(float_expression) --q回以弧度表C的角的正切
cot(float_expression) --q回以弧度表C的角的余切
/***反三角函?**/
asin(float_expression) --q回正u是float 值的以弧度表C的?BR>acos(float_expression) --q回余u是float 值的以弧度表C的?BR>atan(float_expression) --q回正切是float 值的以弧度表C的?BR>atan2(float_expression1,float_expression2)
--q回正切是float_expression1 /float_expres-sion2的以弧度表示的角
degrees(numeric_expression)
--把弧度{换ؓ角度q回与表辑ּ相同的数据类型可?BR> --integer/money/real/float cd
radians(numeric_expression)
--把角度{换ؓ弧度q回与表辑ּ相同的数据类型可?BR> --integer/money/real/float cd
exp(float_expression) --q回表达式的指数?BR>log(float_expression) --q回表达式的自然Ҏ?BR>log10(float_expression)--q回表达式的?0 为底的对数?BR>sqrt(float_expression) --q回表达式的qx?/P>
/***取近似值函?**/
ceiling(numeric_expression)
--q回>=表达式的最整数返回的数据cd与表辑ּ相同可ؓ
--integer/money/real/float cd
floor(numeric_expression)
--q回<=表达式的最整数返回的数据cd与表辑ּ相同可ؓ
--integer/money/real/float cd
round(numeric_expression)
--q回以integer_expression 为精度的四舍五入D回的数据
--cd与表辑ּ相同可ؓinteger/money/real/float cd
abs(numeric_expression)
--q回表达式的l对D回的数据cd与表辑ּ相同可ؓ
--integer/money/real/float cd
sign(numeric_expression)
--试参数的正负号q回0 零? 正数?1 负数q回的数据类?BR> --与表辑ּ相同可ؓinteger/money/real/float cd
pi() --q回gؓπ ?.1415926535897936
rand([integer_expression])
--用Q选的[integer_expression]做种子值得?-1 间的随机点?/P>
----字符串函?---
ascii() --函数q回字符表达式最左端字符的ascii 码?BR>char() --函数用于ascii 码{换ؓ字符
--如果没有输入0 ~ 255 之间的ascii 码值char 函数会返回一个null ?BR>lower() --函数把字W串全部转换为小?BR>upper() --函数把字W串全部转换为大?BR>str() --函数把数值型数据转换为字W型数据
ltrim() --函数把字W串头部的空格去?BR>rtrim() --函数把字W串N的空格去?BR>left(),right(),substring()
--函数q回部分字符?BR>charindex(),patindex()
--函数q回字符串中某个指定的子串出现的开始位|?BR>soundex() --函数q回一个四位字W码
--soundex函数可用来查扑֣音相似的字符?BR> --但soundex函数Ҏ字和汉字均只q回0 ?nbsp;
difference()
--函数q回由soundex 函数q回的两个字W表辑ּ的值的差异
--0 两个soundex 函数q回值的W一个字W不?BR> --1 两个soundex 函数q回值的W一个字W相?BR> --2 两个soundex 函数q回值的W一二个字符相同
--3 两个soundex 函数q回值的W一二三个字W相?BR> --4 两个soundex 函数q回值完全相?BR>
quotename() --函数q回被特定字W括h的字W串
/*select quotename('abc', '{') quotename('abc')
q行l果如下
----------------------------------
{abc} [abc]*/
replicate() --函数q回一个重复character_expression 指定ơ数的字W串
/*select replicate('abc', 3) replicate( 'abc', -2)
q行l果如下
----------- -----------
abcabcabc NULL*/
reverse() --函数指定的字符串的字符排列序颠?BR>replace() --函数q回被替换了指定子串的字W串
/*select replace('abc123g', '123', 'def')
q行l果如下
----------- -----------
abcdefg*/
space() --函数q回一个有指定长度的空白字W串
stuff() --函数用另一子串替换字符串指定位|长度的子串
----数据cd转换函数----
cast() 函数语法如下
cast() (<expression> as <data_ type>[ length ])
convert() 函数语法如下
convert() (<data_ type>[ length ], <expression> [, style])
select cast(100+99 as char) convert(varchar(12), getdate())
q行l果如下
------------------------------------------
199 Jan 15 2000
----日期函数----
day() --函数q回date_expression 中的日期?BR>month() --函数q回date_expression 中的月䆾?BR>year() --函数q回date_expression 中的q䆾?BR>dateadd(<datepart> ,<number> ,<date>)
--函数q回指定日期date 加上指定的额外日期间隔number 产生的新日期
datediff(<datepart> ,<number> ,<date>)
--函数q回两个指定日期在datepart 斚w的不同之?BR>datename(<datepart> , <date>)
--函数以字W串的Ş式返回日期的指定部分
datepart(<datepart> , <date>)
--函数以整数值的形式q回日期的指定部?BR>getdate() --函数以datetime 的缺省格式返回系l当前的日期和时?/P>
----pȝ函数----
app_name() --函数q回当前执行的应用程序的名称
coalesce() --函数q回众多表达式中W一个非null 表达式的?BR>col_length(<'table_name'>, <'column_name'>)
--函数q回表中指定字段的长度?BR>col_name(<table_id>, <column_id>)
--函数q回表中指定字段的名U即列名
datalength() --函数q回数据表达式的数据的实际长?BR>db_id(['database_name'])
--函数q回数据库的~号
db_name(database_id)
--函数q回数据库的名称
host_id() --函数q回服务器端计算机的名称
host_name() --函数q回服务器端计算机的名称
identity(<data_type>[, seed increment]) [as column_name])
--identity() 函数只在select into 语句?BR> --使用用于插入一个identity column列到新表?/P>
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/
isdate() --函数判断所l定的表辑ּ是否为合理日?BR>isnull(<check_expression>, <replacement_value>)
--函数表辑ּ中的null 值用指定值替?BR>isnumeric() --函数判断所l定的表辑ּ是否为合理的数?BR>newid() --函数q回一个uniqueidentifier cd的数?BR>nullif(<expression1>, <expression2>)
--nullif 函数在expression1 与expression2 相等?BR> --q回null D不相{时则返回expression1 的?/P>
====_֦SQL语句====
说明Q复制表(只复制结?源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
说明Q拷贝表(拯数据,源表名:a 目标表名Qb)
SQL: insert into b(a, b, c) select d,e,f from b;
说明Q显C文章、提交h和最后回复时?BR>SQL: select a.title,a.username,b.adddate
from table a,(select max(adddate) adddate from table where table.title=a.title) b
说明Q外q接查询(表名1Qa 表名2Qb)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
说明Q日E安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时?getdate())>5
说明Q两张关联表Q删除主表中已经在副表中没有的信?BR>SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )