【问题标题】:Insert 52 weeks for each year in MySQL (PHP script)在 MySQL 中每年插入 52 周(PHP 脚本)
【发布时间】:2011-04-02 01:39:36
【问题描述】:

我想在一个名为 week_year 的表中使用以下架构:

Week_year = {id, week, year}

插入每年的周数,这样,对于 2001 年,有第 1 周、第 2 周、第 3 周、...、第 52 周,然后从 2002 年重新开始直到 2009 年。

我尝试了不同的 PHP 脚本,但似乎无法做到正确。我尝试了不同的表,一个是一年,一个是一周,然后每周给一个 year_id,但这似乎没有效果。

我希望有人可以帮助我创建一个简单的 PHP 循环来生成这些数字并将它们插入到我的 MySQL 数据库中。

添加了作为答案发布的附加信息

我试过这段代码,一年中无限循环:

<?php

$year = 2001;
$week_start = 1;
$week_end = 52;

for ( $week_start = 1; $week_start <= 52; $week_start++ ) {

echo $week_start;
echo "<br />";
    for ($week_start = 1; $week_start <= 52; $year++) {
        echo $year;
        echo "<br />";
    }
}
?>

【问题讨论】:

  • 字段都是整数吗? id 列是 auto_increment 吗?
  • 你能把你试过的代码和你得到的错误信息贴出来吗?
  • 是的,所有字段都是整数,id 是 A_I。我可以发布一些代码是的,请稍等

标签: php mysql loops for-loop


【解决方案1】:

只需从您的 php 脚本中调用 populate_year_week(2001,2009) 存储过程!

drop table if exists year_week;

create table year_week
(
year_id smallint unsigned not null,
week_id tinyint unsigned not null,
primary key (year_id, week_id)
);

delimiter ;

drop procedure if exists populate_year_week;

delimiter #

create procedure populate_year_week
(
in from_yr smallint unsigned,
in to_yr smallint unsigned
)
proc_main:begin

declare yr smallint unsigned default 0;
declare wk tinyint unsigned default 0;

  truncate table year_week;

  -- put some validation here !
  if to_yr < from_yr then
        leave proc_main;
    end if;

  -- create years and weeks...
  set yr = from_yr;
    while yr <= to_yr do

    set wk = 1;
    while wk < 53 do
      insert into year_week (year_id, week_id) values (yr,wk);    
      set wk=wk+1;
    end while;

    set yr=yr+1;
  end while;

end proc_main #

delimiter ;

call populate_year_week(2001,2009);

select * from year_week order by year_id, week_id;

【讨论】:

    【解决方案2】:

    在 SQL 中执行此操作,而不是 PHP:

    create table artificial_range( int id not null auto_increment, ci int);
    
    insert into artificial_range(c1) values (1);
    

    -- 现在是人工范围的两倍

    insert into artificial_range(c1) select c1 from artificial_range;
    

    -- 再重复上述插入五次; -- 你现在在人工范围中有 64 行

    -- now insert into week_year:
    insert into week_year(week, year)
    select a.id, b.id + 2000
    from 
      artificial_range a,
      artificial_range b 
    where a.id < 53 and b.id < 10;
    
    
    -- or even better, just make week_year a view:
    create view week_year as
    select a.id as week, b.id + 2000 as year
    from 
      artificial_range a,
      artificial_range b 
    where a.id < 53 and b.id < 10;
    

    一个警告:根据您所说的“周”的含义,有些年份有 53 周。

    【讨论】:

    • +1:您将节省 PHP 生成的来自网络服务器的所有流量。这不会,因为它不需要由应用程序代码驱动。
    【解决方案3】:
    $id_start = 1;
    for ($year=2002; $year<=2009; $year++) {
       $values = array();
       for ($week=1; $week<=52; $week++) {
          $values[] = implode(',', array($id_start++, $week, $year));
       }
       $query = 'INSERT INTO `week_year` (id, week, year) ' 
              . 'VALUES (' . implode('),(', $values) . ')';
    
       $db->query($query);
    }
    

    ** 注意 ** 查看localtime() PHP 函数。您不需要有一个带有周/年的表,只需一个时间戳并将该时间戳与$info = localtime($row-&gt;timestamp, true) 一起使用并执行$weekNumber = floor($info['tm_yday'] / 7); 来获取您的周数。只是一个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多