Chinaunix首页 | 论坛 | 博客
  • 博客访问: 371381
  • 博文数量: 166
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1640
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-05 11:44
个人简介

文章不在长,坚持不懈记录下努力前行的脚步

文章分类

全部博文(166)

文章存档

2017年(19)

2016年(59)

2015年(88)

我的朋友

分类: Mysql/postgreSQL

2015-11-23 16:46:08

20151123
主题:msyql批处理脚本实例
(来自MySQL reference manual 3.5)
=============================================
#!/bin/sh
mysql --user='root' --password='mysql123' -h 120.26.81.89 --database='qwifi'>>qwifi.out< SET @start=UNIX_TIMESTAMP();
select * from page_access_record;
SET
@s=@seconds:=90000+UNIX_TIMESTAMP()-@start,
@d=TRUNCATE(@s/86400,0), @s=MOD(@s,86400),
@h=TRUNCATE(@s/3600,0), @s=MOD(@s,3600),
@m=TRUNCATE(@s/60,0), @s=MOD(@s,60),
@day=IF(@d>0,CONCAT(@d,' day'),''),
@hour=IF(@d+@h>0,CONCAT(IF(@d>0,LPAD(@h,2,'0'),@h),' hour'),''),
@min=IF(@d+@h+@m>0,CONCAT(IF(@d+@h>0,LPAD(@m,2,'0'),@m),' min.'),''),
@sec=CONCAT(IF(@d+@h+@m>0,LPAD(@s,2,'0'),@s),' sec.');

SELECT
CONCAT(@seconds,' sec.') AS seconds,
CONCAT_WS(' ',@day,@hour,@min,@sec) AS elapsed;
quit
!!

A more secure way to use the shell.
So that passwords are not embedded in the shell source file create a password file:

echo "batchpassword" > /etc/security/mysqlpassword
chmod 200 /etc/security/mysqlpassword

Then in your script:

echo "update tablex set x=1 where a=2;" | mysql mydb --user=batchdb --password=`cat /etc/security/mysqlpassword`

This assumes you have created a user called "batchdb" with that password and the correct access rights to the database called "mydb".

-- 文档中还有很多用法

主题:去重统计
(来自MySQL reference manual 3.6.8)
=============================================
SELECT
  YEAR,
  MONTH,
  BIT_COUNT(BIT_OR(1 << DAY)) AS days
FROM
  t1
GROUP BY YEAR,
  MONTH ;





阅读(630) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~