Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用prepared语句进行复杂mysql查询时出现问题_Mysql_Variables_Dynamic - Fatal编程技术网

使用prepared语句进行复杂mysql查询时出现问题

使用prepared语句进行复杂mysql查询时出现问题,mysql,variables,dynamic,Mysql,Variables,Dynamic,以下过程不执行。int_id是用户定义的输入 我的目标是使用STR_TABLE_NAME作为表的动态变量。 注意:但是,如果我用目标表的名称替换它并删除SET@prep_stmt=,查询就会工作 为了在准备好的语句中包含变量,我使用了字符串连接。您的查询将出现错误,因为STR_TABLE_NAME将被视为表名,并且找不到任何表名。 从输入参数中删除“backtick”。 我明白了:MySQL说:1054-where子句中的未知列'int_id'。NIDA:“IDTABLE”必须重命名为“ItID

以下过程不执行。int_id是用户定义的输入

我的目标是使用STR_TABLE_NAME作为表的动态变量。 注意:但是,如果我用目标表的名称替换它并删除SET@prep_stmt=,查询就会工作

为了在准备好的语句中包含变量,我使用了字符串连接。您的查询将出现错误,因为STR_TABLE_NAME将被视为表名,并且找不到任何表名。 从输入参数中删除“backtick”。
我明白了:MySQL说:1054-where子句中的未知列'int_id'。NIDA:“IDTABLE”必须重命名为“ItIDID”相同参数,在声明语句后的SELECT语句中从变量ItILID中删除Butik,否则MySQL会将其视为列。我在回答中也提到了同样的问题。使用答案中提到的完整代码,我对此做了许多小的更正。请确保您有一个名为int_id的过程输入参数,请考虑int_id而不是[…]中的idtable更新您的答案,其中id_region=int_id
BEGIN

DECLARE STR_TABLE_NAME VARCHAR(100) DEFAULT NULL;
SELECT `table_source` INTO STR_TABLE_NAME FROM `list_repository` WHERE 
id=`int_id` LIMIT 1;

DROP TABLE IF EXISTS `loyaltytry`;
SET @prep_stmt = CREATE TABLE `loyaltytry` as (SELECT Months as month, 
Number_of_New_Customers as `new_customers` , `Number_of_Repeat_Customers`  
as `repeat_customers`
from
(SELECT  monthname(Months) as Months, month(Months)  as `Month_number`,
sum(CASE WHEN REP_COUNT ='no' then cnts end) as `Number_of_New_Customers`,
sum(CASE WHEN REP_COUNT = 'yes' then cnts end) as 
`Number_of_Repeat_Customers`
from (
SELECT months,REP_COUNT,count(*) as cnts
from (
    SELECT (date_commande_client) as Months , numero,
    CASE WHEN cnt > 1 THEN 'yes'
    ELSE 'no'
    END AS REP_COUNT
    from ( 
    SELECT COUNT(*) as cnt, date_commande_client, numero
    FROM  STR_TABLE_NAME           
        WHERE YEAR(date_commande_client)=2017
        AND intitule IN (SELECT showroom_name FROM `showrooms` WHERE 
id_region=`int_id`)
       group by date_commande_client , numero) as tmp) as final
    GROUP BY Months,REP_COUNT ) as tmp1
GROUP BY monthname(Months),month(Months)
ORDER BY Month_number) as finalll
);

PREPARE stmt FROM @prep_stmt; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt;

END
BEGIN

    DECLARE STR_TABLE_NAME VARCHAR(100) DEFAULT NULL;
    SELECT table_source INTO STR_TABLE_NAME FROM list_repository WHERE id = int_id LIMIT 1;

    DROP TABLE IF EXISTS `loyaltytry`;

    SET @prep_stmt = CONCAT('CREATE TABLE `loyaltytry` AS (
                                        SELECT 
                                            Months AS MONTH, 
                                            Number_of_New_Customers AS `new_customers`, 
                                            `Number_of_Repeat_Customers` AS `repeat_customers`
                                        FROM (
                                            SELECT 
                                                MONTHNAME(Months) AS Months, 
                                                MONTH(Months) AS `Month_number`, 
                                                SUM(CASE WHEN REP_COUNT = "no" THEN cnts END) AS `Number_of_New_Customers`, 
                                                SUM(CASE WHEN REP_COUNT = "yes" THEN cnts END) AS `Number_of_Repeat_Customers`
                                            FROM (
                                                SELECT 
                                                    months,
                                                    REP_COUNT, 
                                                    COUNT(*) AS cnts
                                                FROM (
                                                    SELECT 
                                                        (date_commande_client) AS Months, 
                                                        numero, 
                                                        CASE WHEN cnt > 1 THEN "yes" ELSE "no" END AS REP_COUNT
                                                    FROM (
                                                        SELECT 
                                                            COUNT(*) AS cnt, 
                                                            date_commande_client, 
                                                            numero
                                                        FROM ',  STR_TABLE_NAME , 
                                                        ' WHERE YEAR(date_commande_client) = 2017 
                                                            AND intitule IN (
                                                                SELECT 
                                                                    showroom_name
                                                                FROM `showrooms`
                                                                WHERE id_region= ', int_id , ' 
                                                            )
                                                        GROUP BY date_commande_client, numero
                                                    ) AS tmp
                                                ) AS final
                                                GROUP BY Months, REP_COUNT
                                            ) AS tmp1
                                            GROUP BY MONTHNAME(Months), MONTH(Months)
                                            ORDER BY Month_number) AS finalll
                                        )');

    PREPARE stmt FROM @prep_stmt; 
    EXECUTE stmt; 
    DEALLOCATE PREPARE stmt;

END