MySQL过程结果中的行重复

时间:2018-12-05 13:55:01

标签: mysql duplicates procedure

我的MySQL过程从多个表返回结果。问题是,它经常返回重复的行。

即使Distinct也无济于事。

这里是示例:

duplicate rows

问题是,我在做什么错了?

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetOrderItems`( last_order_id INT )
BEGIN
    SELECT
        DISTINCT items.order_item_id AS `id`,
        items.order_id AS `order_id`,
        items.order_item_name AS `name`,
        items.order_item_type AS `type`,
        qty.meta_value AS `qty`,
        line_total.meta_value AS `line_total`,
        item_note.meta_value AS `line_note`,
        item_options.text AS `options` 
    FROM
        wp_woocommerce_order_items AS items
        LEFT JOIN wp_woocommerce_order_itemmeta AS qty ON items.order_item_id = qty.order_item_id 
        AND `qty`.meta_key = "_qty"
        LEFT JOIN wp_woocommerce_order_itemmeta AS line_total ON items.order_item_id = line_total.order_item_id 
        AND `line_total`.meta_key = "_line_total"
        LEFT JOIN wp_woocommerce_order_itemmeta AS item_note ON items.order_item_id = item_note.order_item_id 
        AND `item_note`.meta_key = "  "
        LEFT JOIN (
        SELECT
            `order_item_id`,
            GROUP_CONCAT( `meta_value` SEPARATOR '; ' ) AS `text` 
        FROM
            wp_woocommerce_order_itemmeta 
        WHERE
            LOWER( `meta_key` ) LIKE '%option%' 
            OR LOWER( `meta_key` ) LIKE '%size%' 
            OR LOWER( `meta_key` ) LIKE '%add extra%' 
        GROUP BY
            `meta_key`,
            `order_item_id` 
        ) item_options ON items.order_item_id = item_options.order_item_id 
    WHERE
        items.order_id > last_order_id;

END

0 个答案:

没有答案