使用不同的值更新多行的同一列
Update same column on multiple rows with different values
我想 运行 每天 cron job
更新 dadmin_reminder_date
、dshipment_date
和 darrival_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);
}
}
?>
请放轻松。我以前从未编写过代码,并获得了一个可以玩得开心的网站。
因此,您可以将更新语句更改为如下所示,并一起删除 select:
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 服务器。 MySQL 中介绍了如何执行此操作。我还为我在名为 Whosebug 的服务器上创建的模式编写了示例代码。
我添加了删除 table 以防你想在任何调整后重新运行 代码。
USE Whosebug;
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 Whosebug;
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 值的需要。
我在您的 table 中添加了一个 dprevious_shipment_date 字段作为日期,这样您就可以跟踪上个月发货的时间,以防您需要它来进行恢复,您可以将其从如果需要,还有更新语句。
我想 运行 每天 cron job
更新 dadmin_reminder_date
、dshipment_date
和 darrival_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);
}
}
?>
请放轻松。我以前从未编写过代码,并获得了一个可以玩得开心的网站。
因此,您可以将更新语句更改为如下所示,并一起删除 select:
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 服务器。 MySQL 中介绍了如何执行此操作。我还为我在名为 Whosebug 的服务器上创建的模式编写了示例代码。
我添加了删除 table 以防你想在任何调整后重新运行 代码。
USE Whosebug;
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 Whosebug;
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 值的需要。
我在您的 table 中添加了一个 dprevious_shipment_date 字段作为日期,这样您就可以跟踪上个月发货的时间,以防您需要它来进行恢复,您可以将其从如果需要,还有更新语句。