`
zl198751
  • 浏览: 273464 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SQL 学习

阅读更多
//在DOS界面中设变量
set @i=0;
update 表名 set 字段名 = concat('all_',(@i:=@i+1)) where name = 'All';

//表的级联操作(select)
select c.字段名, b.id from  表名1 c, 表名2 a, 表名3 b where a.字段名 = c.id and a.字段名 = b.id and b.字段名 like '%all%';

//表的级联操作(update)
update 表名1 a, 表名2 b, 表名3 c set a.字段名 = concat('all_',c.code,b.code) where a.字段名 = b.id and b.字段名 = c.id and a.name = 'all';

//表的级联操作(delete)
delete from 表名 where 字段名 = (select a.id from 表名1 a, 表名2 b where a.字段名 = b.id and b.字段名 = '****') and 字段名 = '****';

//根据条件导出表中内容(仅仅是内容字段值)
select * from 表名 where 字段名 = '****' into outfile 'c:/temp/plan.sql' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'

//根据条件导出表中内容(有insert等操作语句,可以再次导入其他数据库)
先创建临时表
create table rptemp select * from 表名 where 字段名 = '****';
再导出临时表
mysqldump

//无需密码进入MYSQL 先启动MYSQLD
mysqld --skip-grant-tables

//进入MYSQL
mysql -uroot -ppassword -h192.168.22.125 --default-character-set=gbk

//清除binlog
reset master;

//转义BINLOG 的二进制编码到文件中
mysqlbinlog --no-default binlog.000001 > c:\temp\ss.sql

//sql when的用法 类似 IF 语句
select case when version = '5' then id else name end as a from employee;

//union 和 union all
将2个SQL查询出来的不同结果合并成一个结果集合,只要是列的类型相同。
union
select id,name from employee union select id, name from project;

union all
select "1" union all select "1";
显示        1
            1
union会自动合并相同的行,union all不会。

使用union的时候若使用了limit则需要用括号包裹SQL。
错误:select id from employee limit 1 union all select id from employee limit 1;     结果:1个值
正确:(select id from employee limit 1) union all (select id from employee limit 1); 结果:2个值


//导出存储过程
mysqldump -uroot -ppassword -n -d -t -R puppet > c:\temp\procedure.sql

//存储过程
delimiter |
CREATE PROCEDURE procedure1(IN parameter1 INTEGER)
BEGIN
DECLARE variable1 CHAR(10);
END;
|
delimiter ;

//存储过程 处理 exception "CONTINUE HANDLER" "EXIT HANDLER"
CREATE PROCEDURE p23 ()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
SET @x = 1;
INSERT INTO t4 VALUES (1);
SET @x = 2;
INSERT INTO t4 VALUES (1);
SET @x = 3;
End;
|

//存储过程 附值
注意:select中附值是用":=",一般附值set @x=@x+1; set @x:=@x+1;一样暂时认为。
delimiter |
Create procedure myif()
Begin
set @x=1;
select @x;
select (@x:=s1*3) from t;
select @x;
End;
|
delimiter ;

把数据库查询结果附值
select id from t limit 1 into @x;

//存储过程 if..then..elseif..then..else..end if      while..do..end while
注意: elseif是写一块的
delimiter |
Create procedure myifelse5()
Begin
set @x=1;

while @x<=11 do
if @x < 5 then
select concat(@x,"小于五");
set @x = @x + 1;

elseif @x>=5 and @x<=10 then
select concat(@x,"大于等于五,小于等于十");
set @x = @x + 1;

else
select concat(@x,"大于十");
set @x = @x + 1;

end if;
end while;

End;
|
delimiter ;

//存储过程参数in, out, inout
in传入参数,out 用于传出参数, inout都可以使用
set @s1='sss1';
set @s2='sss2';
set @s3='sss3';

delimiter |
create procedure inoutp(in s varchar(255), out b varchar(255), inout c varchar(255))
begin
select c;
select b;
set s='2345';
select s;
set b=s;
set c=s;
select b;
select c;
end
|
delimiter ;

call inoutp(@s1,@s2,@s3);

//触发器 trigger
Create trigger triggerName before insert|update|delete on tableName For each row
begin
sql code.
end

sql code 中OLD.columnName表示操作之前的值
           NEW.columnName表示操作之后的值



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics