0

我有一个简单的 mysql 表:

CREATE TABLE  `cont` (
  `ID` int(11) NOT NULL,
  `Meeting_id` int(11) DEFAULT NULL,
  `member_name` varchar(20) NOT NULL,
  `cont_prod` varchar(20) NOT NULL,
  `start_date` date NOT NULL,
  `type_of` varchar(100),
  `ord_qty` int(11) DEFAULT NULL
) ;

我正在尝试按照此示例将行动态转置为列在此处输入链接描述

  1. 当我在WHERE子句中使用输入参数时,在SELECT STATEMENT旁边,它给了我找不到错误列。

  2. 我试图将变量声明为:

    设置@v1 = c_prod; SET @v2 = s_date;

在 where 子句中:

WHERE cont_prod = @v1 AND start_date = @v2

它仍然无法正常工作

如何在 WHERE 子句中定义输入参数变量?

任何帮助都会受到高度赞赏。

 BEGIN
         SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when type_of = '',
          type_of,
          '' then ord_qty end) ',
          type_of
        )
      ) INTO @sql
    FROM
      cont
      where cont_prod = c_prod AND start_date = s_date;
    SET @sql = CONCAT('SELECT product_id,member_name,start_date,cont_prod, ', @sql, ' 
                      FROM cont WHERE cont_prod = c_prod AND start_date = s_date
                       GROUP BY member_name,cont_prod,start_date');
    
    PREPARE stmt FROM @sql;
    EXECUTE st......
4

1 回答 1

1

用于?在prepared statement中指定参数,传递参数就EXECUTE行了。

SET @sql = CONCAT('SELECT product_id,member_name,start_date,cont_prod, ', @sql, ' 
            FROM cont WHERE cont_prod = ? AND start_date = ?
            GROUP BY member_name,cont_prod,start_date');
PREPARE stmt FROM @sql;
EXECUTE stmt USING c_prod, s_date;
于 2021-06-14T11:34:42.177 回答