简体   繁体   English

用不同的值更新多行中的同一列

[英]Update same column on multiple rows with different values

I want to run a cron job daily to update the dadmin_reminder_date , dshipment_date , and darrival_date . 我想每天运行一次cron job以更新dadmin_reminder_datedshipment_datedarrival_date I need to add icmd_days to each of the current dates. 我需要将icmd_days添加到每个当前日期。

Ex. 例如 For the second row(couldn't upload the image) the output should be old(05/07/2015)/new(06/04/2015),old(05/07/2015)/new(06/04/2015),old(05/15/2015)new(06/12/2015) . 对于第二行(无法上传图片),输出应为old(05/07/2015)/new(06/04/2015),old(05/07/2015)/new(06/04/2015),old(05/15/2015)new(06/12/2015) The icmd_days = 28 for this row. 此行的icmd_days = 28 Each row has a different amount of days or icmd_days value. 每行都有不同的天数或icmd_days值。

Here is the php code that I have so far, however it is updating each row with the same amount of 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);
}
}
?>

Please go easy on me. 请对我好一点。 I have never coded before and was handed a website to have fun with. 我以前从未编码,因此被转到了一个可以玩的网站。

So you can change your update statement to look like this and remove your select all together: 因此,您可以将更新语句更改为如下所示,并一起删除所有选择:

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

Date add automatically adds the number of days in ICMD_DAYS to the shipment_date and sets your date accordingly. 日期添加会自动将ICMD_DAYS中的天数添加到shipping_date中,并相应地设置您的日期。

Also you can remove your today logic for the built in GETDATE() function in SQL: 您还可以删除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

I am guessing you are using MySQL not SQL Server. 我猜您正在使用MySQL,而不是SQL Server。 Here is how to do this in MySQL. 这是在MySQL中执行此操作的方法。 I also wrote out example code for a schema I created on my server called stackoverflow. 我还为在服务器上创建的架构stackoverflow编写了示例代码。

I included the drop table in case you want to re-run the code after any tweaks. 我添加了删除表,以防您在进行任何调整后重新运行代码。

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`;

But the basic code you will need will be this: 但是您将需要的基本代码是:

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 -- */

And this code will remove the need for your loop to set the value of the csubscriptionstatus. 并且此代码将消除您的循环来设置csubscriptionstatus值的需要。

I added a field dprevious_shipment_date to your table as a date so that you can track when the previous month shipment went out in case you need it for recovery purposes you can remove that from the update statement as well if needed. 我在表中添加了一个字段dprevious_shipment_date作为日期,以便您可以跟踪上个月发货的时间,以防万一您需要恢复时也可以将其从更新语句中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM