mysql数据库怎么获得当前日期? - 扬帆☆启航 - 博客园

来源:百度文库 编辑:神马文学网 时间:2024/04/24 13:24:21

我的网站用的是mysql,有一个字段是“日期”,我想在我加入纪录时mysql就将该纪录的“日期”默认值设为当前日期,
试过timestamp,但默认是14位,也就是有年月日时分秒,我只想要年月日,怎么办?
------------------------------------------------------------------------------------
截取撒。截断成年月日
-------------------------------------------------------------------------------------- 有Date类型啊

select curdate()
-------------------------------------------------------------------------------------- 我是直接在mysqlcc里插入纪录的,不用mysql的命令,我希望我每插入一条记录,那条记录的日期就是当前日期,用curdate()可以做到吗?curdate()在mysqlcc里怎么用?
-------------------------------------------------------------------------------------- 呵呵 这么简单
截取 呵呵

-------------------------------------------------------------------------------------- mysql> select now();
mysql> select sysdate();
mysql> select curdate();
mysql> select current_date;
mysql> select curtime();
mysql> select current_time;

try and choose one you need
-------------------------------------------------------------------------------------- kevinliuu(@。@)
我是要设置mysql的默认值为当前日期,而且用的是mysqlcc
-------------------------------------------------------------------------------------- mysqlcc没用过

不管怎么样,你不是通过sql语句插入数据吗? 如果是那就能够解决了

insert into talbe(ddate) values( select now()) ;
-------------------------------------------------------------------------------------- select date(date1) from test
-------------------------------------------------------------------------------------- 如果是通过sql语句那我会,
我现在用mysqlcc图形界面,就像在access里一样,点右键/插入纪录,问题是mysql每个字段都会有一个默认值,而且是0000-00-00,我想让这个默认值是当前日期
-------------------------------------------------------------------------------------- impossible
-------------------------------------------------------------------------------------- 可是timestamp就能做到,呵呵
但是timestamp有时分秒,我不知道如何截取成为只有年月日
-------------------------------------------------------------------------------------- 你是想在建表时用default吧!
-------------------------------------------------------------------------------------- to chouy(chouy)

是的,有方法吗
-------------------------------------------------------------------------------------- The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. See Section 11.3.1.1, “TIMESTAMP Properties as of mysql 4.1”.

If a column definition includes no explicit DEFAULT value, mysql determines the default value as described in Section 11.1.4, “Data Type Default Values”.

BLOB and TEXT columns cannot be assigned a default value.

以上是从mysql的官方网站上抄来的!你看看,一定有用!
-------------------------------------------------------------------------------------- o yeah
-------------------------------------------------------------------------------------- to chouy(chouy)
不识英文,能给汉语吗?
-------------------------------------------------------------------------------------- 以上的大致意思是:

Defalut子句里指定的值必须是常量,不能是一个函数;但有一个例外,就是timestamp类型可以指定默认值为current_timestamp.

我想你的date类型是不能指定你需求的那样的默认值的.
只好在insert into 中用函数了.我想这在编程中应该也不费太大劲儿吧.
-------------------------------------------------------------------------------------- 就是说除了Timestamp能够作为默认值之外,所有其他的函数都不能做为插入数据库的默认值
-------------------------------------------------------------------------------------- 回kevinliuu(@。@) ( ) 信誉:107
-------------------------------------
是只有列的数据类型为 timestamp 时, 才可以指定 default 值为 current_timestamp.
举个例子:
CREATE TABLE `event` (
`something` char(20),
`time` timestamp default CURRENT_TIMESTAMP
)

这个语句在插入一条数据时可以用:
insert into event(something) values('thing1');

插入后的结果:
+-----------+---------------------+
| something | time |
+-----------+---------------------+
| thing1 | 2006-06-29 15:32:40 |
+-----------+---------------------+
-------------------------------------------------------------------------------------- 只要取时间的时候转换一下就可以了

select something,date(time) from event
-------------------------------------------------------------------------------------- to chouy(chouy)
既然这样,那就只能将timestamp字段截取了,mysql默认是timestamp(14),可是怎么截取前8位呢?
好像从mysql4.0开始就没有timestamp(8)了
-------------------------------------------------------------------------------------- to dreamover(梦醒了)

只要取时间的时候转换一下就可以了

select something,date(time) from event

我在jsp里写select 字段1,..字段二,date(time) from 表1 就出错,我的日期字段名就是"date"


-------------------------------------------------------------------------------------- 试一下

select date(`date`) from table1

`是esc下面那个键的输入符号

字段名最好不要用关键字,像date,char,int这样的名称
-------------------------------------------------------------------------------------- to dreamover(梦醒了)
还是不行..
字段名也改了
-------------------------------------------------------------------------------------- 你用的mysql版本号是多少?

可能里面没有date函数吧,看一下帮助,找一下你那个版本里有哪些日期函数,有很多函数可以达到目的


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cchaha/archive/2007/03/29/1544702.aspx