sql >> Databasteknik >  >> RDS >> Mysql

Problem med komplex mysql-fråga med förberedd uttalande

  • För att inkludera variabler i Prepared Statement använde jag strängsammansättning. Din fråga kommer att ge ett fel eftersom STR_TABLE_NAME kommer att behandlas som tabellnamn och hittar inga.
  • Ta bort (`) backtick från indataparametern.
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



  1. Hur man använder metoden sendirect() för att skicka en sökfråga från servlet till jsp

  2. Hibernate Fel vid exekvering av DDL via JDBC Statement

  3. Kan jag torrköra/sandbox sql-kommandon?

  4. Oracle-sekvens som börjar med 2 istället för 1