我有一个名为sagepaydirect__transactions
的表,我正在通过迁移将其response
字段设置为名为response
的表的sagepaydirect__responses
字段。
两个通过名为sagepaydirect__requests
的表格在vendor_tx_code
字段中_分隔字符串的最后4位数字上进行连接。
问题在于sagepaydirect__requests
在加入sagepaydirect__transactions
时会有多行。我想做的是做JOIN,以便UPDATE从sagepaydirect__transactions
中的ID中取值,其值在数字上更高。
这是我对查询的尝试,但投诉是t.id
不存在。
UPDATE sagepaydirect__transactions t
INNER JOIN (
SELECT * FROM sagepaydirect__requests rq
WHERE SUBSTRING_INDEX(rq.vendor_tx_code, '_', -1) = t.id
ORDER BY id DESC
LIMIT 1
) rq_table
ON t.id = SUBSTRING_INDEX(rq_table.vendor_tx_code, '_', -1)
INNER JOIN sagepaydirect__responses rs
ON rq.id = rs.request_id
SET t.response = rs.status_detail
答案 0 :(得分:1)
您正尝试在from
子句中使用相关子查询。那不行。以下使用聚合来获取最大id
,然后继续使用join
。
UPDATE sagepaydirect__transactions t INNER JOIN
(SELECT SUBSTRING_INDEX(rq.vendor_tx_code, '_', -1) as val, max(id) as maxid
FROM sagepaydirect__requests rq
WHERE SUBSTRING_INDEX(rq.vendor_tx_code, '_', -1) = t.id
group by id SUBSTRING_INDEX(rq.vendor_tx_code, '_', -1)
) rq
ON t.id = rq.val INNER JOIN
sagepaydirect__responses rs
ON rq.id = rs.request_id
SET t.response = rs.status_detail;