简体   繁体   English

用另一个表的字符串化值替换表中的 substring

[英]Replace substring from an table with an stringified value of another table

So I have 2 tables and one contains an string with an ID I want to replace with an string by another table.所以我有 2 个表,其中一个包含一个带有 ID 的字符串,我想用另一个表的字符串替换它。
I came up with this SQL, which should work, but it seems like an LEFT JOIN isn't allowed in this case.我想出了这个 SQL,它应该可以工作,但在这种情况下似乎不允许使用 LEFT JOIN。

UPDATE sales_channel_api_context AS api
SET api.payload = REPLACE(
    api.payload,
    SUBSTRING(
        api.payload,
        LOCATE('paymentMethodId":"', api.payload)+18,
        32
    ),
    LOWER(HEX(c.default_payment_method_id))
)
LEFT JOIN customer AS c
ON c.id = api.customer_id
WHERE api.payload LIKE '%paymentMethodId%' AND api.customer_id IS NOT NULL;

Does anyone know an SQL Query that does exactly this, without creating another table?有谁知道一个 SQL 查询可以做到这一点,而无需创建另一个表?
An temp table can be used but an new permanent table is no solution.可以使用临时表,但新的永久表不是解决方案。

ChatGPT gave me a working solution and it is as follow: ChatGPT 给了我一个可行的解决方案,如下所示:

UPDATE sales_channel_api_context 
JOIN customer c ON c.id = sales_channel_api_context.customer_id
SET payload = 
    CASE 
    WHEN payload LIKE '%paymentMethodId%' THEN
        REPLACE(
            payload,
            SUBSTRING(
                payload,
                LOCATE('paymentMethodId":"', payload) + 18,
                32
            ),
            LOWER(HEX(c.default_payment_method_id))
        )
    ELSE payload
    END
WHERE  sales_channel_api_context.customer_id IS NOT NULL;

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

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