【问题标题】:data not coming in proper order from table using php使用php从表中未按正确顺序排列数据
【发布时间】: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 的问题?缩小您的问题范围
  • 您的日期字段是什么数据类型?你在里面放了什么数据?为此,您需要使用 DATEDATETIME 字段。
  • 数据类型为 DATETIME fot date_updated。问题是在插入表格时,位于提要顶部的文章比迟到的文章更早地被送入。所以,当我做一个选择时,最后的文章是最先出现的,这是不应该发生的

标签: php mysql zend-framework sql-order-by on-duplicate-key


【解决方案1】:

使用下面的查询

SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 0,10

在限制之后,我们需要传递偏移量和要获取的记录数。

【讨论】:

  • 我试过这个但没有用。问题在于插入数据。我想以相反的顺序插入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2018-04-21
  • 2021-12-26
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
相关资源
最近更新 更多