MySQL学习笔记

mysql服务器安装配置:
1. sudp apt-get install mysql-server-5.0
2. 创建库second_proxy, 为secProxy添加用户:grant all privileges on second_proxy.* to secProxy@'%' identified by 'passwd';
3. 设置mysql.user表如下:
+-----------+------------------+-------------------------------------------+
| Host      | User             | Password                                  |
+-----------+------------------+-------------------------------------------+
| %         | root             | *D0CC270F9B91B4CF3D13B5D56E1E52A9F7C001B1 |
| localhost | debian-sys-maint | *F6484D4F45B78E2CE99845B5D54DED451834E427 |
| %         | secProxy         | *B67F64B96B21AA1CDEE83C49A6DB23837F095FD9 |
+-----------+------------------+-------------------------------------------+
临时数据:localhost    | root             | *D0CC270F9B91B4CF3D13B5D56E1E52A9F7C001B1 |

4. sudo vim /etc/mysql/my.cnf, 修改bind-address = 0.0.0.0
5. 重起sudo /etc/init.d/mysql restart

报表事例:
http://dev.mysql.com/tech-resources/articles/ddws/index.html
http://www.gizmola.com/blog/archives/63-PHPlot,-MySQL-and-the-Dark-Ages-of-Camelot.html

查询一天:
select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate();

查询一周:
select * from table  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);

查询一个月:
select * from table  where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);

以每十分钟为一组
set @timeBegin='2006-10-27 10:00:00';
set @interval=60*10;
select FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(operationTime)/@interval)*@interval) as timeNearTo,count(*) as 'count'
from data
where data.operationTime>@timeBegin
group by timeNearTo
order by timeNearTo desc;

sql语句if语句的使用:
SELECT distinct(if(source>16383, source&16383, source))
FROM `verify_log_archive` order by source asc

case语句(类似oracle的decode):
select now() as query_time,
CASE verify_type
WHEN 1 THEN 'http'
WHEN 2 THEN 'https'
WHEN 3 THEN 'socks4'
WHEN 4 THEN 'socks5'
ELSE 'unknown' end as verify_type, count(*)
from proxy_server where area_code=1 group by verify_type


----------------------------mysql 的date_format----------------------------------
DATE_FORMA T(date, format) 根据格式串format 格式化日期或日期和时间值date,返回结果串。
可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值,以便得到所希望的格式。根据format字符串格式化date值:
%S, %s      两位数字形式的秒( 00,01, . . ., 59)
%i              两位数字形式的分( 00,01, . . ., 59)
%H             两位数字形式的小时,24 小时(00,01, . . ., 23)
%h, %I       两位数字形式的小时,12 小时(01,02, . . ., 12)
%k             数字形式的小时,24 小时(0,1, . . ., 23)
%l              数字形式的小时,12 小时(1, 2, . . ., 12)
%T            24 小时的时间形式(h h : m m : s s)
%r             12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p            AM 或P M
%W           一周中每一天的名称( S u n d a y, Monday, . . ., Saturday)
%a            一周中每一天名称的缩写( Sun, Mon, . . ., Sat)
%d            两位数字表示月中的天数( 00, 01, . . ., 31)
%e            数字形式表示月中的天数( 1, 2, . . ., 31)
%D            英文后缀表示月中的天数( 1st, 2nd, 3rd, . . .)
%w            以数字形式表示周中的天数( 0 = S u n d a y, 1=Monday, . . ., 6=Saturday)
%j             以三位数字表示年中的天数( 001, 002, . . ., 366)
%U           周(0, 1, 52),其中Sunday 为周中的第一天
%u            周(0, 1, 52),其中Monday 为周中的第一天
%M           月名(J a n u a r y, February, . . ., December)
%b            缩写的月名( J a n u a r y, February, . . ., December)
%m           两位数字表示的月份( 01, 02, . . ., 12)
%c            数字表示的月份( 1, 2, . . ., 12)
%Y            四位数字表示的年份
%y            两位数字表示的年份
%%           直接值“%”

select date_format(日期字段,'%Y-%m-%d') as '日期' from test

--------------
查询结果联合:
select 'Danvor' as Name
UNION
select 'Kitty'  as Name
-----------------------------------------------------

一个存储过程事例:插入一条记录,并计算平均值插入最后
--drop procedure second_proxy.sp_test1;

create PROCEDURE second_proxy.sp_test1(in p_name varchar(10), in p_age int)
begin  
     insert into test(name,age) values(p_name, p_age);
     select avg(age) into @age_avg from test;
     delete from test where name='everage';
     insert into test(name,age) values('everage',@age_avg);
end;

call sp_test1('honglie', 40);

select * from test order by id asc

类型转换:
select cast('5000' as unsigned integer);
-------------------------------------------------------
导入导出数据:
导出:
select ip,
       port,
       case verify_type
       when 0 then 0
       when 1 then 1
       when 2 then 2
       when 3 then 4
       when 4 then 8
       when 5 then 16
       else 0 end as verify_type,1,user_name,pass_word  into OUTFILE '/other/ip.txt'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '/n'
from `second_proxy`.`proxy_server_ok`
where usable=1;

导入:LOAD DATA LOW_PRIORITY INFILE  '/other/ip.txt'  REPLACE INTO TABLE Orders FIELDS TERMINATED BY ',' ENCLOSED BY '"';


清除日志: purge master logs before '2008-09-18';
查看存储过程:select `name` from mysql.proc where db='second_proxy2' and `type` = 'PROCEDURE'
查看版本:select version();

---------------------------------------------
表分片脚本:
#!/bin/bash

#$YEARWEEK=`date +%Y%W`
YEARWEEK="`date +%Y%W`"
YEARWEEK_ADD1=`expr $YEARWEEK + 1`
YEARWEEK_ADD2=`expr $YEARWEEK + 2`
DB_USER=active_front
DB_PWD=ncs123

TABLE1_SQL="ALTER TABLE discover_log ADD PARTITION (PARTITION discover_log_$YEARWEEK_ADD1 VALUES LESS THAN ($YEARWEEK_ADD2));"
TABLE2_SQL="ALTER TABLE verify_log_archive ADD PARTITION (PARTITION verify_log_archive_$YEARWEEK_ADD1 VALUES LESS THAN ($YEARWEEK_ADD2));"

echo $TABLE1_SQL | mysql -u $DB_USER -p$DB_PWD active_front
echo $TABLE2_SQL | mysql -u $DB_USER -p$DB_PWD active_front

exit 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值