MySQL - 博客文章调度系统

发布于 2024-09-24 09:38:32 字数 451 浏览 1 评论 0原文

我正在使用 CodeIgniter 创建一个博客文章调度系统。我希望每天显示 10 个帖子。 posts 表中有一个名为 scheduled_date 的字段,我将获取小于或等于当前日期的帖子。当管理员用户向数据库添加新记录时,我需要一个 SQL 语句来帮助我计算数据库中最新日期的记录数。例如:

// 9 records returned for the date 2011-01-01
$numbers_of_records == 9;
if($numbers_of_records == 10){
    // inserts record with `scheduled_date`='2011-01-01'
}else{
    // inserts record with the date latest date +1 day
}

我如何有效地完成这个任务?

谢谢

I am creating a blog post scheduling system using CodeIgniter. I want 10 posts to show up a day. There is a field in the posts table named scheduled_date which I will get the posts that are less than or equal to the current date. When an admin user adds a new record to the database, I need an SQL statement that somehow will help me COUNT the number of records with the latest date in the database. For example:

// 9 records returned for the date 2011-01-01
$numbers_of_records == 9;
if($numbers_of_records == 10){
    // inserts record with `scheduled_date`='2011-01-01'
}else{
    // inserts record with the date latest date +1 day
}

How would I efficiently accomplish this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-10-01 09:38:32

这样就可以解决问题了。它简单而高效。

<?php

//  It is very bad to have floating values, especially for settings
//  it is good to use some sort of factory or settings class
$maxDailyPosts = (int) SettingsFactory::getSettings()->get('maxDailyPosts');
$date = '2011-01-01';

//  Load # of post for data
$numberOfRecords = (int) getNumberOfPostPerDate($date);

//  Figure out the interval for increment
$dayInterval = ($numberOfRecords >= $maxDailyPosts ) ? 1 : 0;

//  
$query = "INSERT INTO tbl (publish_date, ...) VALUES (DATE_ADD('$date', INTERVAL $dayInterval DAY), ...)";

?>

This will do the trick. It is simple and efficient.

<?php

//  It is very bad to have floating values, especially for settings
//  it is good to use some sort of factory or settings class
$maxDailyPosts = (int) SettingsFactory::getSettings()->get('maxDailyPosts');
$date = '2011-01-01';

//  Load # of post for data
$numberOfRecords = (int) getNumberOfPostPerDate($date);

//  Figure out the interval for increment
$dayInterval = ($numberOfRecords >= $maxDailyPosts ) ? 1 : 0;

//  
$query = "INSERT INTO tbl (publish_date, ...) VALUES (DATE_ADD('$date', INTERVAL $dayInterval DAY), ...)";

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文