我的困境是我需要在我的事务表中将subscription_id更新为基于相同billingid的新subscription_id。
我的订阅表包含来自不同账单的多个订阅记录的成员。我很难将这个更新选择/连接放在mysql上。
TBL subscription_id
+--------------------+---------------+----------+ | subscription_id | memberid | billerid | +--------------------+---------------+----------+ 12345 | A | ? | +--------------------+---------------+----------+ 67890 | A | 13 | +--------------------+---------------+----------+ 11222 | B | 13 | +--------------------+---------------+----------+ 91422 | B | ? | +--------------------+---------------+----------+
TBL交易
+---------------+-----------------+----------+ | trans_id | subscription_id | billerid | +---------------+-----------------+----------+ 00012 | 12345 | 13 | +---------------+-----------------+----------+ 02013 | 12345 | 13 | +---------------+-----------------+----------+ 08013 | 11222 | 13 | +---------------+-----------------+----------+ 50013 | 11222 | 13 | +---------------+-----------------+----------+
答案 0 :(得分:1)
UPDATE transaction AS t
JOIN (SELECT s1.subscription_id AS old_subid,
s2.subscription_id AS new_subid,
s2.billerid AS billerid
FROM subscription_id AS s1
JOIN subscription_id AS s2 ON s1.memberid = s2.memberid
WHERE s1.billerid = '?'
AND s2.billerID != '?') AS s
ON t.billerid = s.billerid AND t.subscription_id = s.old_subid
SET t.subscription_id = s.new_subid
我想我想出了一个更适合你最新评论的简单版本:
UPDATE transaction AS t
JOIN subscription_id AS s1
ON s1.subscription_id = t.subscription_id
AND s1.billerid != t.billerid
JOIN subscription_id AS s2
ON s1.memberid = s2.memberid
AND s2.subscription_id != s1.subscription_id
AND s2.billerid = t.billerid
SET t.subscription_id = s2.subscription_id