0

我需要修改以下代码以删除值为 1xxxxxxxx 和 3xxxxxxxx 的所有内容(例如,100000001、100000002 等),但我对 SQL 的了解并不完美。有没有人可以帮我修改这个脚本?

SET @increment_id = '100000001';

SELECT *
FROM `sales_flat_quote_address_item`
WHERE `sales_flat_quote_address_item`.`quote_address_id` IN ( 
    SELECT `address_id` 
        FROM `sales_flat_quote_address` 
        WHERE `sales_flat_quote_address`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
            )
    );

DELETE  
FROM `sales_flat_quote_shipping_rate`
WHERE `sales_flat_quote_shipping_rate`.`address_id` IN ( 
    SELECT `address_id` 
        FROM `sales_flat_quote_address` 
        WHERE `sales_flat_quote_address`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
            )
    );



DELETE 
FROM `sales_flat_quote_address`
WHERE `sales_flat_quote_address`.`quote_id` IN ( 
    SELECT `entity_id`
        FROM `sales_flat_quote`
        WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
    );


DELETE FROM `sales_flat_quote_item_option`
WHERE `sales_flat_quote_item_option`.`item_id`  IN ( 
    SELECT `item_id` 
        FROM `sales_flat_quote_item` 
        WHERE `sales_flat_quote_item`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
            )
    );


DELETE FROM `sales_flat_quote_item`
WHERE `sales_flat_quote_item`.`quote_id` IN ( 
    SELECT `entity_id`
        FROM `sales_flat_quote`
        WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
    );


DELETE FROM `sales_flat_quote_payment`
WHERE `sales_flat_quote_payment`.`quote_id` IN ( 
    SELECT `entity_id`
        FROM `sales_flat_quote`
        WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
    );


DELETE FROM `sales_flat_quote`
WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id;


DELETE FROM `sales_order_datetime`
WHERE `sales_order_datetime`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order`
        WHERE `sales_order`.`increment_id` = @increment_id
    );


DELETE FROM `sales_order_decimal`
WHERE `sales_order_decimal`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order`
        WHERE `sales_order`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_int`
WHERE `sales_order_int`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order`
        WHERE `sales_order`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_text`
WHERE `sales_order_text`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order`
        WHERE `sales_order`.`increment_id` = @increment_id
    );


DELETE FROM `sales_order_varchar`
WHERE `sales_order_varchar`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order`
        WHERE `sales_order`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_entity_datetime`
WHERE `sales_order_entity_datetime`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order_entity`
        WHERE `sales_order_entity`.`increment_id` = @increment_id
    );


DELETE FROM `sales_order_entity_decimal`
WHERE `sales_order_entity_decimal`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order_entity`
        WHERE `sales_order_entity`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_entity_int`
WHERE `sales_order_entity_int`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order_entity`
        WHERE `sales_order_entity`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_entity_text` 
WHERE `sales_order_entity_text`.`entity_id` IN ( 
    SELECT `entity_id`
        FROM `sales_order_entity`
        WHERE `sales_order_entity`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_entity_varchar`
WHERE `sales_order_entity_varchar`.`entity_id`  IN ( 
    SELECT `entity_id`
        FROM `sales_order_entity`
        WHERE `sales_order_entity`.`increment_id` = @increment_id
    );

DELETE FROM `sales_order_entity`
WHERE `sales_order_entity`.`increment_id` = @increment_id;

DELETE FROM `sales_order`
WHERE `sales_order`.`increment_id` = @increment_id;
4

3 回答 3

0

改变

WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id

WHERE `sales_flat_quote`.`reserved_order_id` BETWEEN 100000000 AND 199999999

等待!标题说100000000-199999999,但问题说1xxxxxxxx and 3xxxxxxxx,所以如果是后者,则将其更改为:

WHERE `sales_flat_quote`.`reserved_order_id` BETWEEN 100000000 AND 399999999

或者,您可以选择使用REGEXP运算符:

WHERE `sales_flat_quote`.`reserved_order_id` REGEXP '^[123][[:digit:]]{8}'
于 2012-10-13T16:01:46.063 回答
0

通常,我会建议这样做。但是,如果性能不是问题,您可以使用:

...
WHERE LEFT(`sales_order`.`increment_id`, 1) = 1 
    AND RIGHT(`sales_order`.`increment_id`, 8) BETWEEN 0 and 99999999

...
WHERE LEFT(`sales_flat_quote`.`reserved_order_id`, 1) = 1 
    AND RIGHT(`sales_flat_quote`.`reserved_order_id`, 8) BETWEEN 0 and 99999999

这是假设字段长度为 9 个字符。它将匹配最左边的值1并检查右边集合的范围。

也许您可能还想使用事务以防出现问题?

于 2012-10-13T17:38:17.667 回答
0

您可以使用以下脚本删除所有以 1 或 3 开头的记录:

DELETE 
FROM 'sales_flat_quote_address_item'
WHERE
'sales_flat_quote_address_item'.'quote_address_id' like '1%'
OR 
'sales_flat_quote_address_item'.'quote_address_id' like '3%';
于 2012-10-13T21:35:42.617 回答