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 に答える