【问题标题】:Issue when updating date in db table更新数据库表中的日期时出现问题
【发布时间】:2012-01-07 21:44:37
【问题描述】:

我正在处理一个 codeigniter 项目,我正在尝试解决一个 sql 问题。我有一个查询更新了我表中的日期字段,但它根本没有更新它。

我有这张桌子

CREATE TABLE `Customer` (
  `customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(55) NOT NULL DEFAULT '',
  `last_name` varchar(55) NOT NULL DEFAULT '',
  `city` varchar(255) DEFAULT '',
  `state` char(2) DEFAULT '',
  `zip` int(5) DEFAULT NULL,
  `title` varchar(255) NOT NULL DEFAULT '',
  `image` varchar(255) DEFAULT '',
  `blurb` blob,
  `end_date` date DEFAULT NULL,
  `goal` int(11) DEFAULT NULL,
  `paypal_acct_num` int(11) DEFAULT NULL,
  `progress_bar` enum('full','half','none') DEFAULT NULL,
  `page_views` int(11) NOT NULL DEFAULT '0',
  `total_pages` int(11) NOT NULL DEFAULT '0',
  `total_conversions` int(11) NOT NULL DEFAULT '0',
  `total_given` int(11) NOT NULL DEFAULT '0',
  `conversion_percentage` int(11) NOT NULL DEFAULT '0',
  `avg_contribution` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

当我运行这个查询来插入数据时,它运行良好并将日期设置为2012-11-01

INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');

然后我得到 customer_id 并尝试运行此查询

UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';

并将日期 end_date 字段设置为0000-00-00

为什么将结束日期更改为 0000-00-00 而不是 2012-14-01?

【问题讨论】:

    标签: php sql codeigniter


    【解决方案1】:

    2012-14-01 是第十四个月的第一天 :)

    (因此它的无效日期,因此被转换为0000-00-00Data truncated for column 'end_date' at row 1 警告被mysql 返回,您可以通过在不良行为查询后立即向mysql 查询SHOW WARNINGS 来查看)

    2012-01-14 是 1 月 14 日。

    【讨论】:

    • 美国人可能会感到困惑 :p 标准日期格式曾经是 YYYY/MM/DD :)
    • @Prof83: 2012-01-14 YYYY-MM-DD
    • 好吧,你是说日期的 mysql 格式是 yyyy-mm-dd ?
    • 完全正确。它通常是将日期存储为字符串的最佳格式,因为它使用正常的字典顺序正确排序
    • @Andriy M 对不起哈哈哈我的意思是 YYYY/DD/MM
    【解决方案2】:

    使用这个:

     UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
    

    使用日期函数更新此字段。

    【讨论】:

    • 我不想把今天的日期放在那里。
    • date('Y-m-d', $timestamp) - 阅读 PHP 时间戳
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多