MYSQL SQL编程实现
时间:2010-09-01 来源:pcsh
需求:
1.实现上个月的数据全部导入到这个月,并且时间要相应进行调整,即2010-08-10 10:10:11 转化为2010-09-10 10:10:11;
2.如何实现导入效率最高(表有20个字段左右,记录将近30W);
3.如何修改某张表的流水号,让其重新排列,或让某几行的流水号按我们想要的编号进行排列,相当于让其中几行的行顺序对调。
简单思路:
1.
复制表操作:
create table tbl1 as select * from tbl2;
按两个月相差天数计算:
select date_add(@dt,interval 31 day);
按两个月相差月数计算:
select date_add(@dt,interval 1 month);
转换为数值型:
select unix_timestamp(date_add(@dt,interval 1 month));
update tbl1 set dt=date_add(@dt,interval 1 month);
按相差秒数来计算:
+---------------------------------------+
| unix_timestamp('2010-07-16 10:10:10') |
+---------------------------------------+
| 1279246210 |
+---------------------------------------+
1 row in set (0.22 sec)
mysql> select unix_timestamp('2010-08-16 10:10:10');
+---------------------------------------+
| unix_timestamp('2010-08-16 10:10:10') |
+---------------------------------------+
| 1281924610 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select (1281924610- 1279246210);
+--------------------------+
| (1281924610- 1279246210) |
+--------------------------+
| 2678400 |
+--------------------------+
1 row in set (0.00 sec)
mysql> select from_unixtime(1279246210+2678400);
+-----------------------------------+
| from_unixtime(1279246210+2678400) |
+-----------------------------------+
| 2010-08-16 10:10:10 |
+-----------------------------------+
1 row in set (0.00 sec)
mysql>
注意:以下导出方式是导入到服务器机器上的目录而非本地客户端的目录中
mysql> select tablename into outfile 'c:c.txt' from dbaudit_index;
ERROR 1086 (HY000): File 'c:c.txt' already exists
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> notee
Outfile disabled.
mysql> select tablename into outfile 'c:/d.txt' from dbaudit_index;
Query OK, 14 rows affected (0.00 sec)
mysql> select tablename into outfile 'c:e.txt' from dbaudit_index;
Query OK, 14 rows affected (0.00 sec)
mysql> exit
Bye
导出一张表。字段以|分隔并用"括起来
mysql> select tablename from dbaudit_index into outfile 'c:f.txt' fields termi
nated by '|' enclosed by '"';
Query OK, 14 rows affected (0.02 sec)