【问题标题】:Programmatically fill a column with year-month values [closed]以编程方式用年月值填充一列[关闭]
【发布时间】:2014-12-19 15:16:02
【问题描述】:

如何以编程方式用年月值填充表格列?

这是目标:

DATE
2014-12
2014-11
2014-10
..
2014-02
2014-01
2013-12
2013-11

等等,回到一个设定的日期,例如2008-01

【问题讨论】:

  • 什么编程语言?
  • 对不起,忘了说 - PHP
  • 1) 循环 2) 插入查询。
  • 在循环中使用 SQL 查询是个好习惯吗?

标签: php mysql sql sql-insert


【解决方案1】:

您可以使用 PDO 并循环几年和几个月:

$START_YEAR = 2008;
$END_YEAR = 2012;
$dbh = new PDO('mysql:host=localhost;dbname=mydatabase', $username, $password);
$stmt = $dbh->prepare("INSERT INTO my_table VALUES (?)");
for ($year = START_YEAR; $year <= END_YEAR; ++$year) {
    for ($month = 1; $month <= 12; ++$month) {
        $stmt->bindParam (1, "${year}-${month}");
        $stmt->execute();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多