UPDATE JOIN,错误的数据插入

时间:2012-07-05 22:13:16

标签: php mysql sql

所以我有这两个表, operators_payments AS op 填充了数据,但 op.date_paid NULL ,直到付款日期到来,当发生这种情况时, payment_process AS pp 表用于初始化付款(pp.date_started设置为NOW()),然后付款完成 op.date_paid 设置为 pp.date_started 。显示的查询用于执行此操作,一切都很好,但当所有记录更新时,其中一条记录只有一条获得 op.date_paid 有不同的时间,特别是第二部分,例如(时间设置为除了一个以外的所有部分:2012-07-05 17:28: 14 ,时间设置为1 :2012-07-05 17:28: 02 )。

我使用Mysql 5.5,列具有相同的类型(TIMESTAMP)。 我需要这个,因为我需要的日期与pp.date_started中的日期完全相同。

我的问题是,为什么会发生这种情况,我该怎样做才能实现这一点?

UPDATE operators_payments AS op
    JOIN payment_process AS pp
        ON op.operator_id = pp.operator_id
        AND pp.type = 0
        AND pp.status = 1
SET op.date_paid = pp.date_started, pp.status = 2, pp.message=CONCAT(SUBSTRING_INDEX(message, '|', 1), '| was completed successfully!')
    WHERE op.operator_id = {$this->operator_id}
        AND op.date_paid IS NULL
        AND op.date_end <= pp.date_accounted


+---------------+-----------------------+------+-----+-------------------+----------------+
| Field         | Type                  | Null | Key | Default           | Extra          |
+---------------+-----------------------+------+-----+-------------------+----------------+
| payment       | int(10) unsigned      | NO   | PRI | NULL              | auto_increment |
| operator_id   | int(10) unsigned      | NO   | MUL | 0                 |                |
| date_paid     | timestamp             | YES  | MUL | NULL              |                |
| date_start    | timestamp             | YES  |     | NULL              |                |
| date_end      | timestamp             | YES  | MUL | NULL              |                |
| amount        | decimal(6,4) unsigned | NO   |     | 0.0000            |                |
+---------------+-----------------------+------+-----+-------------------+----------------+


+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field          | Type         | Null | Key | Default           | Extra                          |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| operator_id    | int(11)      | NO   | PRI | NULL              |                             |
| type           | tinyint(4)   | NO   | PRI | NULL              |                             |
| date_started   | timestamp    | YES  |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| date_accounted | timestamp    | YES  |     | NULL              |                             |
| amount         | decimal(6,4) | YES  |     | NULL              |                             |
| status         | tinyint(4)   | YES  | MUL | 0                 |                             |
| message        | varchar(255) | YES  |     | NULL              |                             |
+----------------+--------------+------+-----+-------------------+-----------------------------+

1 个答案:

答案 0 :(得分:1)

我对pay_process上date_started上的on update CURRENT TIMESTAMP子句持怀疑态度......我实际上并不确定它在此查询中可以做什么,但是您在此查询中更新该表,并使用该值。我也不喜欢名为date_started的列的语义不一致,它在每次更新时都会更改它...但我不知道它是如何使用的。我会评估该列是否有必要使用该子句,看看你是否在没有它的情况下得到这种奇怪的行为,

相关问题