使用存储参数中的输入参数作为 where 子句中的变量 MySQL
Using the input parameters from stored parameters as variables inside where clause MySQL
我有一个简单的 mysql table:
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
) ;
我正在尝试按照此示例将行动态转置为列 enter link description here
在 SELECT STATEMENT 旁边,当我在 WHERE 子句中使用输入参数时,它是给我找不到错误列。
我试图将变量声明为:
SET @v1 = c_prod;
SET @v2 = s_date;
where 子句中的 teh:
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......
使用?
在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;
我有一个简单的 mysql table:
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
) ;
我正在尝试按照此示例将行动态转置为列 enter link description here
在 SELECT STATEMENT 旁边,当我在 WHERE 子句中使用输入参数时,它是给我找不到错误列。
我试图将变量声明为:
SET @v1 = c_prod; SET @v2 = s_date;
where 子句中的 teh:
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......
使用?
在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;