将MySQL中sql运行结果保存到文件
有两种方法。
方法一:在mysql>提示符中使用tee
mysql> tee output.txt
Logging to file 'output.txt'
mysql> notee
Outfile disabled.
或者
mysql> \T output.txt
Logging to file 'output.txt'
mysql> \t
Outfile disabled.
这个类似于sqlplus的spool功能,可以将命令行中的结果保存到外部文件中。如果指定已经存在的文件,则结果会附加到文件中。
参考:http://www.ningoo.net
方法二:使用mysql命令行工具的--tee参数
$mysql --tee=ot.txt
Logging to file 'ot.txt'
mysql>
这回将所有的输入和输出内容都记录到指定的文件中(直到exit为止)。如果指定已经存在的文件,则结果会附加到文件中。
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
定义游标
DECLARE cur_名称 CURSOR
FOR
SELECT 字段1,字段2,...字段n from 表 where 字段1=变量;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET 变量1=null,变量2=null;
OPEN cur_名称;
FETCH cur_名称 INTO 变量1,变量2...;
WHILE ( 变量1 IS NOT NULL) DO
&nbs ......
LAST_INSERT_ID
自动返回最后一个 INSERT 或 UPDATE 操作为 AUTO_INCREMENT 列设置的第一个发生的值. 参考这里
The ID that was generated is maintained in the server on a per-connection basis.
LAST_INSERT_ID是基于单个connection的, 不可能被其它的客户端连接改变。
可以用 SELECT LAST_INSERT_ID(); 查询LAST ......
cdate是datetime类型的字段
统计一年的如下
select datepart(yy,cdate) as '月份',sum(cmoney) from consumption group by datepart(yy,cdate)
统计一月的如下
select datepart(mm,cdate) as '月份',sum(cmoney) from consumption where datepart(yy,cdate)=2009 group by datepart(mm,cdate)
统计一周 ......