简体   繁体   English

MySQL更新查询不起作用

[英]MySQL update query not working

I have a MySQL table which has the following columns 我有一个MySQL表,其中包含以下各列

urlByCustomer table
----------------------------------------------
|customerID | TeamID | date | numUrlsConsumed|
----------------------------------------------
|           |        |      |                | 
----------------------------------------------

urlmapping

Column  Type    Null    Default Comments    MIME
urlMappingID    bigint(20)  No           
customerID  int(11) Yes     0        
activityID  int(11) Yes     0        
contactID   int(11) Yes     0        
fullURL mediumtext  Yes     NULL         
lastModified    timestamp   No  CURRENT_TIMESTAMP        
developerSandbox    varchar(25) Yes              

I've got this code that is being executed to update the table 我已经执行了此代码以更新表

 $start = strtotime(date('Y-m-d 00:00:00'));
                             $end = strtotime(date('Y-m-d 23:59:59'));
                             $countAllThisGuysVals = "SELECT COUNT( DISTINCT`customerID`,`fullURL`)  
                                                      FROM `urlmapping` 
                                                      WHERE `urlMappingID` >= $ORIGINAL_COPY 
                                                      AND `customerID` = $currentCustomerID";
                            $countTheVals= $conn->query($countAllThisGuysVals);
                            $countedfinal =0;
                            foreach ($countTheVals as $countRow) {
                                $countedfinal = array_sum($countRow)/2;
                            }
                                $tableUpdateQuery = "UPDATE `urlByCustomer`
                                                SET `date` = NOW()
                                                WHERE `customerID`= $currentCustomerID AND 
                                                      UNIX_TIMESTAMP(`date`) BETWEEN '{$start}' and '{$end}'";

                                $conn->exec($tableUpdateQuery);

                                 $tableUpdateQuery = "UPDATE `urlByCustomer`
                                                SET `numUrlsConsumed` = $countedfinal
                                                WHERE `customerID`= $currentCustomerID AND 
                                                      UNIX_TIMESTAMP(`date`) BETWEEN '{$start}' and '{$end}'";
                                $conn->exec($tableUpdateQuery);
                                echo "update path <br>";
                                $tableModified = true;
                                $originalLPID++;

Variables are pretty much all declared, but the declarations are spread out, so I'm just posting this part to shorten it. 几乎所有变量都已声明,但是声明分散了,所以我只是发布此部分以缩短它。 The update query to the date column seems to be working, but the second update isn't. 对日期列的更新查询似乎有效,但第二次更新无效。 It worked 17 minutes ago though, so I'm confused since the only thing that changed between the next test was that I added some new values that should be causing it to update that column. 但是它在17分钟前起作用了,所以我感到困惑,因为在下一个测试之间唯一改变的是我添加了一些新值,这些新值应该导致它更新该列。

Idk. Idk。 I guess one possiblity could be the UNIX_TIMESTAMP. 我猜可能是UNIX_TIMESTAMP。 I'm running this in Parallels on a Mac, so I'm not sure what that translates to for timestamps. 我正在Mac上的Parallels中运行此程序,因此我不确定时间戳的含义。

It looks like you're changing the value of "date" in your first update, without considering the fact that your second update will now not find the same rows in the WHERE clause (as you've just changed the date). 似乎您正在更改第一次更新中的“日期”的值,而没有考虑到您的第二次更新现在将不会在WHERE子句中找到相同的行(因为您刚刚更改了日期)。

You can do the updates in a single statement: : 您可以在一个语句中进行更新:

UPDATE urlByCustomer
SET `date` = NOW()
, numUrlsConsumed = $countedfinal
WHERE customerID= $currentCustomerID 
AND UNIX_TIMESTAMP(`date`) BETWEEN '{$start}' and '{$end}'

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

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