在同一个表

时间:2016-03-08 17:25:43

标签: mysql select sql-update

UPDATE inventory_audit_scans
SET    inventory_audit_scans.inventory_item_id = 
(SELECT id FROM (SELECT inventory_item.id as id
        FROM   inventory_item 
               RIGHT JOIN products 
                       ON products.id = inventory_item.product_id 
               LEFT JOIN barcodes 
                      ON barcodes.product_id = inventory_item.product_id 
               LEFT JOIN products_skus 
                      ON products_skus.product_id = inventory_item.product_id 
               LEFT OUTER JOIN inventory_audit_scans ias 
                            ON ias.inventory_item_id = inventory_item.id 
                               AND ias.audit_report_id = 
                                   inventory_audit_scans.audit_report_id 
        WHERE  ( products.sku LIKE inventory_audit_scans.text
                  OR barcodes.barcode LIKE inventory_audit_scans.text
                  OR products_skus.sku LIKE inventory_audit_scans.text 
                  OR inventory_item.serial LIKE inventory_audit_scans.text ) 
               AND inventory_item.store_id = in_store_id 
               AND inventory_audit_scans.inventory_item_id IS NULL 
               AND ( ( ( products.category_id = 
                         inventory_audit_scans.category_id ) 
                       AND ( inventory_audit_scans.category_id IS NOT NULL ) ) 
                      OR ( ( ( products.category_id = 1 ) 
                              OR ( products.category_id = 2 ) ) 
                           AND inventory_audit_scans.category_id IS NULL ) ) 
        ORDER  BY inventory_item.id ASC 
        LIMIT  1) TmpTbl), 
       inventory_audit_scans.status_id = IF(inventory_audit_scans.inventory_item_id , 1 , 2) ,
       updated = Now() 
WHERE  inventory_audit_scans.audit_report_id = 1 

在内部选择中我收到错误消息

Error Code: 1054. Unknown column 'inventory_audit_scans.text' in 'where clause'

有没有办法可以在随附的SELECT中访问update语句的实际行,以确定哪个是我尚未使用的下一个可用空闲ID?

在通过以下方法尝试之前,大约需要3分钟来“审核”1600条记录:

CREATE PROCEDURE `compareaudit`(IN audit_id INT(11))
BEGIN
DECLARE bDone INT DEFAULT 0;
DECLARE current_record_id INT(11) DEFAULT NULL;
DECLARE current_category_id INT(11) DEFAULT NULL; 
DECLARE current_text VARCHAR(50) DEFAULT NULL;
DECLARE current_store_id INT(11) DEFAULT NULL;
DECLARE current_audit_id INT(11) DEFAULT NULL;

DECLARE res_inventory_item_id INT(11) DEFAULT NULL;
DECLARE res_serial VARCHAR(50) DEFAULT NULL;
DECLARE res_sku VARCHAR(20) DEFAULT NULL;
DECLARE res_barcode VARCHAR(30) DEFAULT NULL;
DECLARE res_product_skus VARCHAR(100) DEFAULT NULL;

DECLARE cur CURSOR FOR
    SELECT `id`, `category_id`, `text`, `store_id`, `audit_report_id` FROM inventory_audit_scans WHERE audit_report_id = audit_id AND item_status IS NULL ORDER BY id ASC;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;

OPEN cur;

myloop:loop

    FETCH cur INTO current_record_id, current_category_id, current_text, current_store_id, current_audit_id;

    IF bDone = 1 THEN
        leave myloop;
    END IF;

    call find_item(current_category_id, current_text, current_store_id, current_audit_id, res_inventory_item_id, res_serial, res_sku, res_barcode, res_product_skus);

    IF (res_inventory_item_id IS NOT NULL) THEN
        UPDATE inventory_audit_scans SET inventory_item_id = res_inventory_item_id, item_status = 1, updated = NOW() WHERE id = current_record_id;
    END IF;

    IF (res_inventory_item_id IS NULL) THEN
        UPDATE inventory_audit_scans SET inventory_item_id = NULL, item_status = 2, updated = NOW() WHERE id = current_record_id;
    END IF;

end loop myloop;

select true;
END

CREATE PROCEDURE `find_item`(
    IN in_category_id INT(11), 
    IN in_text VARCHAR(50), 
    IN in_store_id INT(11),
    IN in_audit_id INT(11),
    OUT my_inventory_item_id INT(11),
    OUT my_serial VARCHAR(50),
    OUT my_sku VARCHAR(20),
    OUT my_barcode VARCHAR(30),
    OUT my_product_skus VARCHAR(100))
BEGIN
    SELECT          inventory_item.id     AS inventory_item_id, 
                    inventory_item.`serial` AS `serial`, 
                    products.sku          AS SKU, 
                    barcodes.barcode      AS barcode, 
                    products_skus.sku     AS upc 
    INTO            my_inventory_item_id, 
                    my_serial, 
                    my_sku, 
                    my_barcode, 
                    my_product_skus 
    FROM            inventory_item 
    RIGHT JOIN      products 
    ON              products.id = inventory_item.product_id 
    LEFT JOIN       barcodes 
    ON              barcodes.product_id = inventory_item.product_id 
    LEFT JOIN       products_skus 
    ON              products_skus.product_id = inventory_item.product_id 
    LEFT OUTER JOIN inventory_audit_scans 
    ON              inventory_audit_scans.inventory_item_id = inventory_item.id 
    AND             inventory_audit_scans.audit_report_id = in_audit_id 
    WHERE           ( 
                                    products.sku LIKE in_text 
                    OR              barcodes.barcode LIKE in_text 
                    OR              products_skus.sku LIKE in_text 
                    OR              inventory_item.serial LIKE in_text ) 
    AND             inventory_item.store_id = in_store_id 
    AND             inventory_audit_scans.inventory_item_id IS NULL 
    AND             (
                        ((products.category_id = in_category_id) AND (in_category_id IS NOT NULL))
                    OR
                        (((products.category_id = 1) OR (products.category_id = 2)) AND in_category_id IS NULL)
                    )
    ORDER BY        inventory_item.id ASC 
    LIMIT           1;
END

0 个答案:

没有答案