更新联接,数据插入错误


UPDATE JOIN, wrong data insertion

所以我有两个表,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,时间设置为一个:2012-07-05 17:28:02)。

我使用 Mysql 5.5,列具有相同的类型(时间戳)。我需要这个,因为我需要日期与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              |                             |
+----------------+--------------+------+-----+-------------------+-----------------------------+

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