今天需要在表中用到毫秒级别的时间,可是上网查了一下说MYSQL没有毫秒的数据类型。下面是程序中使用到的一个表结构:
create table ci_cdc.td_cols
(
app_name varchar(60) not null comment '应用(项目)名称'
,tb_name varchar(60) not null comment '表名 '
,col_name varchar(60) not null comment '列名 '
,col_type varchar(60) not null comment '列的类型'
,col_format varchar(100) comment '列格式的正则'
,comment varchar(255) not null comment '列描述 '
,ins_date timestamp not null default current_timestamp comment '数据插入表中的具体时间'
,constraint tb_col_pk primary key(app_name,tb_name,col_name)
)
engine=innodb comment='列级别元数据';我在查询的时候,为什么加了order by ins_date以后结果就不一样了呢?三行数据中的ins_date 列的值都是一样的啊,也没有精确到毫秒。
补充一下,这三行数据插入的顺序确实和加了order by ins_date以后查询出来的结果相同。不过我不知道这是什么原因。mysql> select * from td_cols;
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| app_name | tb_name | col_name | col_type | col_format | comment | ins_date |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| test | crm | id | int | NULL | the primary key | 2011-09-25 13:19:46 |
| test | crm | ins_date | datetime | \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} | the date ins dw | 2011-09-25 13:19:46 |
| test | crm | name | varchar(255) | NULL | the stu name | 2011-09-25 13:19:46 |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
3 rows in set (0.00 sec)mysql> select * from td_cols order by ins_date;
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| app_name | tb_name | col_name | col_type | col_format | comment | ins_date |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| test | crm | id | int | NULL | the primary key | 2011-09-25 13:19:46 |
| test | crm | name | varchar(255) | NULL | the stu name | 2011-09-25 13:19:46 |
| test | crm | ins_date | datetime | \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} | the date ins dw | 2011-09-25 13:19:46 |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
3 rows in set (0.00 sec)
mysql> select microsecond(ins_date) from td_cols;
+-----------------------+
| microsecond(ins_date) |
+-----------------------+
| 0 |
| 0 |
| 0 |
+-----------------------+
3 rows in set (0.00 sec)
create table ci_cdc.td_cols
(
app_name varchar(60) not null comment '应用(项目)名称'
,tb_name varchar(60) not null comment '表名 '
,col_name varchar(60) not null comment '列名 '
,col_type varchar(60) not null comment '列的类型'
,col_format varchar(100) comment '列格式的正则'
,comment varchar(255) not null comment '列描述 '
,ins_date timestamp not null default current_timestamp comment '数据插入表中的具体时间'
,constraint tb_col_pk primary key(app_name,tb_name,col_name)
)
engine=innodb comment='列级别元数据';我在查询的时候,为什么加了order by ins_date以后结果就不一样了呢?三行数据中的ins_date 列的值都是一样的啊,也没有精确到毫秒。
补充一下,这三行数据插入的顺序确实和加了order by ins_date以后查询出来的结果相同。不过我不知道这是什么原因。mysql> select * from td_cols;
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| app_name | tb_name | col_name | col_type | col_format | comment | ins_date |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| test | crm | id | int | NULL | the primary key | 2011-09-25 13:19:46 |
| test | crm | ins_date | datetime | \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} | the date ins dw | 2011-09-25 13:19:46 |
| test | crm | name | varchar(255) | NULL | the stu name | 2011-09-25 13:19:46 |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
3 rows in set (0.00 sec)mysql> select * from td_cols order by ins_date;
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| app_name | tb_name | col_name | col_type | col_format | comment | ins_date |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
| test | crm | id | int | NULL | the primary key | 2011-09-25 13:19:46 |
| test | crm | name | varchar(255) | NULL | the stu name | 2011-09-25 13:19:46 |
| test | crm | ins_date | datetime | \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} | the date ins dw | 2011-09-25 13:19:46 |
+----------+---------+----------+--------------+-------------------------------------+-----------------+---------------------+
3 rows in set (0.00 sec)
mysql> select microsecond(ins_date) from td_cols;
+-----------------------+
| microsecond(ins_date) |
+-----------------------+
| 0 |
| 0 |
| 0 |
+-----------------------+
3 rows in set (0.00 sec)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。