我有一个像这样的mysql存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `accounts_summary_by`(
IN **created_by** int(10)
)
BEGIN
SET group_concat_max_len=2048;
SET @sql = NULL;
SELECT
GROUP_CONCAT(
DISTINCT CONCAT(
'MAX(IF(fiscal_year = ''',
fiscal_year,
''', amount, 0)) AS ',
CONCAT("'",fiscal_year,"'")
)
) INTO @sql
FROM
net_savings;
SET @sql = CONCAT('SELECT accounts.id,
accounts.account,
accounts.region,
accounts.cluster,
accounts.industry, ,'
,@sql,
'FROM net_savings join accounts
on accounts.id = net_savings.account_id
Where accounts.user_id = **created_by**
GROUP BY accounts.account,net_savings.account_id
ORDER BY accounts.id');
PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END
但是在调用这样的程序时:
CALL accounts_summary_by(2)
2是对另一个名为users的表的user_id引用。
它给了我一个错误。请帮助,因为我找不到任何修复我的问题。
0 72 23:41:12 CALL `buckets`.`accounts_summary_by`(1) Error Code: 1054. Unknown column 'created_by' in 'where clause' 0.000 sec
答案 0 :(得分:1)
MySQL的内部编程语言不是php,它不会解析文本中的变量,所以你需要将它正确地连接到预处理语句的中间:
SET @sql = CONCAT('SELECT accounts.id,
accounts.account,
accounts.region,
accounts.cluster,
accounts.industry,'
,@sql,
'FROM net_savings join accounts
on accounts.id = net_savings.account_id
Where accounts.user_id ='
,created_by
,'GROUP BY accounts.account,net_savings.account_id
ORDER BY accounts.id');
由于created_by是过程的参数,因此您无需使用@。
预先定位它