【发布时间】:2013-09-24 10:11:21
【问题描述】:
我正在尝试使用 Zend 将来自提要的数据插入数据库,并在表中添加一个 date_updated 列,以便每当提要中的文章更新时,该列也会更新。但问题是,第一篇文章是先插入的,然后再插入其他文章。因此,当我尝试根据 date_updated DESC 选择前 10 篇文章时,最后插入的文章会排在首位,如果我使用 ASC,那么较旧的文章会被选中。请建议我如何进行。我正在写的查询是:
$sql = "INSERT INTO news_article
(original_article_id, headline,summary, keywords, link, section, topic, date_published, date_updated, content, source_id)
VALUES (?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
original_article_id = ?,
headline = ?,
summary = ?,
keywords = ?,
link = ?,
section = ?,
topic = ?,
date_published = ?,
date_updated = ?,
content = ?,
source_id = ?";
$values = array(
"original_article_id"=>$id,
"headline"=>$item->title,
"summary"=>$summary,
"keywords"=>$keywords,
"link"=>$item->link,
"section"=>"property",
"topic"=>"property",
"date_published"=>$formattedPubDate,
"date_updated"=>$currentDate,
"content"=>$data,
"source_id"=>"3"
);
$result = $db->query(
$sql,
array_merge(array_values($values), array_values($values))
);
然后我正在使用
SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 10
【问题讨论】:
-
那么
date_updated是什么数据类型? INSERT/UPDATE 的问题是没有设置正确的日期,还是 SELECT 的问题?缩小您的问题范围 -
您的日期字段是什么数据类型?你在里面放了什么数据?为此,您需要使用
DATE或DATETIME字段。 -
数据类型为 DATETIME fot date_updated。问题是在插入表格时,位于提要顶部的文章比迟到的文章更早地被送入。所以,当我做一个选择时,最后的文章是最先出现的,这是不应该发生的
标签: php mysql zend-framework sql-order-by on-duplicate-key