MySql 5.7试图在视图上使用变量来模拟“ ROW_NUMBER”

时间:2019-01-13 14:00:58

标签: mysql

我试图将查询从MySql 8.0迁移到5.7,但是当我尝试使用5.7版本时,不支持row_number,然后我尝试使用变量来模拟它,但是我只是意识到视图无法使用变量,该如何运行?

SELECT
    @row_number:=CASE
        WHEN @customer_no = variant_id AND @retail_no = retail_dealer_id THEN @row_number + 1
        ELSE 1
    END AS `row`,
    `test`.`id` AS `id`,
    `test`.`product_discount_id` AS `product_discount_id`,
    @customer_no:=`test`.`variant_id` AS `variant_id`,
    `test`.`color_id` AS `color_id`,
    `test`.`credit_discount_amount` AS `credit_discount_amount`,
    `test`.`product_discount_amount` AS `product_discount_amount`,
    `test`.`main_dealer_type` AS `main_dealer_type`,
    `test`.`color_code` AS `color_code`,
    `test`.`color_name` AS `color_name`,
    @retail_no:=`test`.`retail_dealer_id` AS `retail_dealer_id`,
    `test`.`retail_dealer_name` AS `retail_dealer_name`,
    `test`.`main_dealer_id` AS `main_dealer_id`,
    `test`.`credit_created_at` AS `credit_created_at`,
    `test`.`product_created_at` AS `product_created_at`
FROM
    (
    SELECT
    a.id as a_id,
    f.id as f_id,
        `b`.`id` AS `id`,
        `c`.`id` AS `product_discount_id`,
        `a`.`id` AS `variant_id`,
        `a`.`color_id` AS `color_id`,
        `b`.`discount_amount` AS `credit_discount_amount`,
        `c`.`discount_amount` AS `product_discount_amount`,
        `d`.`main_dealer_type` AS `main_dealer_type`,
        `e`.`code` AS `color_code`,
        `e`.`name` AS `color_name`,
        `f`.`id` AS `retail_dealer_id`,
        `f`.`name` AS `retail_dealer_name`,
        `d`.`md_id` AS `main_dealer_id`,
        `b`.`created_at` AS `credit_created_at`,
        `c`.`created_at` AS `product_created_at`
--      row_number ( ) OVER ( PARTITION BY `a`.`id`, `f`.`id` ) AS `row` 
    FROM
        (
            (
                (
                    (
                        ( `product_color` `a` LEFT JOIN `credit_discount` `b` ON ( ( `b`.`variant_id` = `a`.`id` ) ) )
                        LEFT JOIN `product_discount` `c` ON ( ( `c`.`variant_id` = `b`.`variant_id` ) ) 
                    )
                    LEFT JOIN `product_variant` `d` ON ( ( `d`.`id` = `a`.`product_variant_id` ) ) 
                )
                LEFT JOIN `color_master` `e` ON ( ( `e`.`id` = `a`.`color_id` ) ) 
            )
            LEFT JOIN `retail_dealers` `f` ON ( ( ( `f`.`id` = `b`.`retail_dealer_id` ) OR ( `f`.`id` = `c`.`retail_dealer_id` ) ) ) 
        ) 
    WHERE
        ( `f`.`id` IS NOT NULL ) 
    ) `test` 
HAVING
    ( `row` = 1 )

谢谢!

0 个答案:

没有答案