使用不同的值更新多行上的相同列

时间:2015-05-27 21:06:18

标签: php sql multiple-columns

我想每天运行cron job来更新dadmin_reminder_datedshipment_datedarrival_date。我需要在每个当前日期添加icmd_days

实施例。对于第二行(无法上传图像) 输出应该是 old(05/07/2015)/new(06/04/2015),old(05/07/2015)/new(06/04/2015),old(05/15/2015)new(06/12/2015)。此行的icmd_days = 28。每行都有不同的天数或icmd_days值。

这是我到目前为止的php代码,但是它使用相同的天数更新每一行。

require '../commonincludefiles.php';

global $conn;

$row_string = '';

$Today = date("Y-m-d");

global $conn;



$sub_query = "SELECT bisubscriptionID,dshipment_date,darrival_date,icmd_days,dadmin_reminder_date,vsubscriptionstatus FROM tbl_subscription WHERE darrival_date <= '".$Today."' AND edelete='0' AND vsubscriptionstatus='Shipped'";
$sub_res = mysqli_query($conn,$sub_query);
    $temp = array();
    if (mysqli_num_rows($sub_res) >0) {
    $i=0;
    while($row =  mysqli_fetch_array($sub_res)){
        $temp[$i]['bisubscriptionID'] = $row['bisubscriptionID'];
        $temp[$i]['icmd_days'] = $row['icmd_days'];
        $temp[$i]['new_dshipment_date'] = date('Y-m-d', strtotime($row['dshipment_date'] . '+ '.$row['icmd_days'].'days'));
        $i++;



$new_vsubscriptionstatus = 'Active';


$update_query = "UPDATE tbl_subscription SET vsubscriptionstatus='" . $new_vsubscriptionstatus . "',dshipment_date='" . $new_dshipment_date . "',tmodifydate=NOW() WHERE darrival_date <= '".$Today."' AND edelete='0' AND vsubscriptionstatus='Shipped' AND bisubscriptionID= $bisubscriptionID";
$update_res = mysqli_query($conn, $update_query);
}
}
?>

请放轻松我。我之前从未编过代码,并被交给了一个网站,以获得乐趣。

1 个答案:

答案 0 :(得分:0)

因此,您可以将更新语句更改为如下所示并同时删除所有选择:

UPDATE tbl_subscription 
SET vsubscriptionstatus = '" . $new_vsubscriptionstatus . "'
    ,dshipment_date=DATEADD(d,icmd_days,shipment_date)
    ,tmodifydate=NOW() 
WHERE darrival_date <= '".$Today."' 
AND edelete='0' 
AND vsubscriptionstatus='Shipped' 
AND bisubscriptionID= $bisubscriptionID

日期添加会自动将ICMD_DAYS中的天数添加到shipment_date并相应地设置日期。

此外,您可以删除SQL中内置GETDATE()函数的当前逻辑:

UPDATE tbl_subscription 
SET vsubscriptionstatus = '" . $new_vsubscriptionstatus . "'
    ,dshipment_date=DATEADD(d,icmd_days,shipment_date)
    ,tmodifydate=NOW() 
WHERE darrival_date <= GETDATE()
AND edelete='0' 
AND vsubscriptionstatus='Shipped' 
AND bisubscriptionID= $bisubscriptionID

我猜你使用的是MySQL而不是SQL Server。以下是在MySQL中如何做到这一点。我还为我在服务器上创建的架构(称为stackoverflow)编写了示例代码。

我包含了drop table,以防你想在任何调整后重新运行代码。

USE stackoverflow;

DROP TABLE `tbl_subscription`;

CREATE TABLE `tbl_subscription` (
  `dshipment_date` DATE NULL,
  `icmd_days` INT NULL,
  `darrival_date` DATETIME NULL,
  `edelete` BIT NULL,
  `csubscriptionstatus` VARCHAR(45) NULL,
  `bisubscriptionID` BIGINT NULL,
  `dadmin_reminder_date` DATE NULL,
  `dprevious_shipment_date` DATE NULL,
  `tmodifydate` DATETIME NULL
  )
ENGINE = InnoDB;


INSERT INTO `tbl_subscription`
    (
    dshipment_date
    ,icmd_days
    ,darrival_date
    ,edelete
    ,csubscriptionstatus
    ,bisubscriptionID
    ,dadmin_reminder_date
    ,tmodifydate
    )

SELECT 
    '2015-04-30' as dshipment_date
    ,30 as icmd_days
    ,null as darrival_date
    ,0 as edelete
    ,'Shipped' as csubscriptionstatus
    ,20150001 as bisubscriptionID
    ,null as dadmin_reminder_date
    ,NOW() as tmodifydate 
UNION ALL
SELECT 
    '2015-05-10' as dshipment_date
    ,25 as icmd_days
    ,null as darrival_date
    ,0 as edelete
    ,'Shipped' as csubscriptionstatus
    ,20150002 as bisubscriptionID
    ,null as dadmin_reminder_date
    ,NOW() as tmodifydate 
UNION ALL 
SELECT 
    '2015-04-10' as dshipment_date
    ,30 as icmd_days
    ,'2015-04-13' as darrival_date
    ,0 as edelete
    ,'Shipped' as csubscriptionstatus
    ,20150003 as bisubscriptionID
    ,null as dadmin_reminder_date
    ,NOW() as tmodifydate 
UNION ALL 
SELECT 
    '2015-05-25' as dshipment_date
    ,20 as icmd_days
    ,'2015-05-28' as darrival_date
    ,0 as edelete
    ,'Active' as csubscriptionstatus
    ,20150004 as bisubscriptionID
    ,null as dadmin_reminder_date
    ,NOW() as tmodifydate 
UNION ALL 
SELECT 
    '2015-04-27' as dshipment_date
    ,20 as icmd_days
    ,'2015-04-30' as darrival_date
    ,0 as edelete
    ,'Shipped' as csubscriptionstatus
    ,20150005 as bisubscriptionID
    ,null as dadmin_reminder_date
    ,NOW() as tmodifydate 
;

SELECT * FROM `tbl_subscription`;

SELECT dshipment_date,icmd_days,DATE_ADD(dshipment_date, INTERVAL icmd_days DAY) FROM `tbl_subscription`;


UPDATE `tbl_subscription`
SET dprevious_shipment_date = dshipment_date
    ,csubscriptionstatus = 'Active'
    ,dshipment_date=DATE_ADD(dshipment_date, INTERVAL icmd_days DAY)
    ,tmodifydate = NOW()
WHERE darrival_date <= NOW()
AND edelete='0' 
AND csubscriptionstatus='Shipped' 
/*AND bisubscriptionID= $bisubscriptionID -- this is not needed since we are not doing the loop anymore -- */
;

SELECT * FROM `tbl_subscription`;

但您需要的基本代码是:

USE stackoverflow;
UPDATE `tbl_subscription`
SET dprevious_shipment_date = dshipment_date
    ,csubscriptionstatus = 'Active'
    ,dshipment_date=DATE_ADD(dshipment_date, INTERVAL icmd_days DAY)
    ,tmodifydate = NOW()
WHERE darrival_date <= NOW()
AND edelete='0' 
AND csubscriptionstatus='Shipped' 
/*AND bisubscriptionID= $bisubscriptionID -- this is not needed since we are not doing the loop anymore -- */

此代码将消除循环设置csubscriptionstatus值的需要。

我在您的表格中添加了一个字段dprevious_shipment_date作为日期,以便您可以跟踪上个月发货的时间,以防您需要进行恢复,如果需要,您也可以从更新语句中删除它。