set select语句中的两个变量-mysql

blmhpbnm  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我使用下面的代码,这是在mysql执行,但给错误,而通过java程序,因为java程序不能读取分号。。。对于java,有3条语句。我需要执行这个查询(设置两个变量,然后在一个查询中选择):

set @row_number:=0;set @PROMOTION_ID_NO:='';
SELECT 
     @row_number:=CASE 
        WHEN @PROMOTION_ID_NO=PD.PROMOTION_ID THEN @row_number + 1
        ELSE 1
    END AS SEQ,
     @PROMOTION_ID_NO:=PD.PROMOTION_ID AS PROMOTION_ID,
    PD.CONDITION_CODE,
    PM.PROMOTION_code,
    PD.CONDITION_TYPE
FROM
    POS_PROMOTION_DISCOUNT PD , POS_PROMOTION_MASTER PM WHERE 
    PD.PROMOTION_ID = PM.PROMOTION_ID
AND PD.STORE_NO = 'G121';
lhcgjxsq

lhcgjxsq1#

你可以移动 SET 语句,并执行 CROSS JOIN 将该表与其他表合并。
请不要使用旧的基于逗号的隐式联接,而使用现代的显式联接 Join 基于语法。我已经改成使用 JOIN .. ON 相反。
请尝试以下操作:

SELECT 
     @row_number:=CASE 
                    WHEN @PROMOTION_ID_NO=PD.PROMOTION_ID THEN @row_number + 1
                    ELSE 1
                  END AS SEQ,
     @PROMOTION_ID_NO:=PD.PROMOTION_ID AS PROMOTION_ID,
    PD.CONDITION_CODE,
    PM.PROMOTION_code,
    PD.CONDITION_TYPE
FROM
    POS_PROMOTION_DISCOUNT PD 
JOIN POS_PROMOTION_MASTER PM ON PD.PROMOTION_ID = PM.PROMOTION_ID
CROSS JOIN (SELECT row_number:=0, @PROMOTION_ID_NO:='') AS user_init 
WHERE 
  PD.STORE_NO = 'G121';

相关问题