2016-08-02 35 views
0
下面

条件是程序问题与解析参数mysql的过程 - 在条款

CREATE DEFINER=`root`@`localhost` PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
    select 
    TRUNCATE(sum(price)*in_hours*no_of_days*no_of_sitters,2) as 
    total_amount 
    from job_prices jp 
    join kids_ages ka on ka.id = jp.kids_age_id 
where ka.age in(age) and start_hours > in_hours 
    AND in_hours <= end_hours; 
END 

的问题是在此过程中是怎么会把年龄为varchar(100),在参数,在第 目前我使用的查询

CALL `usitterz`.`sitter_price`(4.10,'1,2,3,4', 3, 5); 

解析但这是错误的,因为在查询读这就像在(“1,2,3,4”),但我想它喜欢 - 在(1,2,3, 4)。

它会像

CREATE DEFINER=`root`@`localhost` PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
    select 
    TRUNCATE(sum(price)*in_hours*no_of_days*no_of_sitters,2) as 
    total_amount 
    from job_prices jp 
    join kids_ages ka on ka.id = jp.kids_age_id 
where ka.age in(1,2,3,4) and start_hours > in_hours 
    AND in_hours <= end_hours; 
END 

回答

1

第一步:勾搭得到的字符串适合的EXECUTE

DROP PROCEDURE IF EXISTS sitter_price; 
DELIMITER $ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
    SET @theSql=CONCAT('SELECT TRUNCATE(sum(price)*',in_hours,'*',no_of_days,'*',no_of_sitters,',2)'); 
    SET @theSql=CONCAT(@theSql,' as total_amount from job_prices jp join kids_ages ka on ka.id = jp.kids_age_id'); 
    SET @theSql=CONCAT(@theSql,' where ka.age in(',age,') and start_hours > ',in_hours,' AND '); 
    SET @theSql=CONCAT(@theSql,in_hours,'<= end_hours'); 
    /* 
    select 
    TRUNCATE(sum(price)*in_hours*no_of_days*no_of_sitters,2) as 
    total_amount 
    from job_prices jp 
    join kids_ages ka on ka.id = jp.kids_age_id 
where ka.age in(1,2,3,4) and start_hours > in_hours 
    AND in_hours <= end_hours; 
    */ 
    select @theSql; 
END$ 
DELIMITER ; 

第二步:传递参数,看看长什么样串像

call sitter_price(89,'1,2,4,8',11,12); 

SELECT TRUNCATE(sum(price)*89*12*11,2) as total_amount 
from job_prices jp 
join kids_ages ka on ka.id = jp.kids_age_id 
where ka.age in(1,2,4,8) and start_hours > 89 
AND 89<= end_hours 

第3步:使用PREPARE和敲定存储过程它。

DROP PROCEDURE IF EXISTS sitter_price; 
DELIMITER $ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
    SET @theSql=CONCAT('SELECT TRUNCATE(sum(price)*',in_hours,'*',no_of_days,'*',no_of_sitters,',2)'); 
    SET @theSql=CONCAT(@theSql,' as total_amount from job_prices jp join kids_ages ka on ka.id = jp.kids_age_id'); 
    SET @theSql=CONCAT(@theSql,' where ka.age in(',age,') and start_hours > ',in_hours,' AND '); 
    SET @theSql=CONCAT(@theSql,in_hours,'<= end_hours'); 
    PREPARE stmt from @theSql; -- create a prepared stmt from the above concat 
    EXECUTE stmt; -- run it 
    DEALLOCATE PREPARE stmt; -- cleanup 
END$ 
DELIMITER ; 

MySqL手册页SQL Syntax for Prepared Statements

请注意,上述CONCAT()只有用户变量(带有@符号)才会成功。不是具有DECLARE的本地变量。

+0

你的做法是好的,但它不是为我工作的答案。给出错误或空结果。 CALL'sitter_price2'(4,'1,2,3,4',1,5); - 给予结果 如果小时数> 4,它给出了错误的结果 CALL'sitter_price2'(5,'1,2,3,4',1,5); - 给零null –

+0

所有重要的是,它产生的SQL字符串正是它应该的。然后它执行它。所以,一个人会在第一步工作,以获得正确的结果。当这是正确的,问题是你的数据。 – Drew

+0

换句话说,EXECUTE执行你想要的查询。如果得到错误的数字,那么你的数据不是你所期望的。 – Drew

0

下面是我自己的问题

CREATE PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
select 
    TRUNCATE(sum(price)*in_hours*no_of_days*no_of_sitters,2) as total_amount 
    from job_prices jp 
    join kids_ages ka on ka.id = jp.kids_age_id 
where 
    jp.status = 1 and 
    find_in_set(ka.age,age) and 
    in_hours between jp.start_hours and jp.end_hours; 
END 

呼叫过程

CALL `usitterz`.`sitter_price`(6, '1,2,3,4', 1, 5); 
+0

你不会使用索引。我会。正如我在[这个其他答案](http://stackoverflow.com/a/38002986)解释 – Drew

+0

如果你编写的代码以'find_in_set()'结尾作为一个解决方案,你可能做错了什么,应该问就像在Stack上一样。并不是每个人都像我一样坐在一起并优化事情。他们可能很高兴告诉你想要你想听到的。 – Drew

+0

德鲁你为什么这么生气。你的sp没有给出正确的结果。你的sp可能是对的。但没有给我结果。留下你的电子邮件ID我会发送表格与数据,然后你可以让你是谁对你或我。 –

0

另一个答案

CREATE DEFINER=`root`@`localhost` PROCEDURE `sitter_price`(
    in in_hours float, 
    in age varchar(100), 
    in no_of_sitters int, 
    in no_of_days int 
) 
BEGIN 
    SET @theSql=CONCAT('SELECT TRUNCATE(sum(price)*',in_hours,'*',no_of_days,'*',no_of_sitters,',2)'); 
    SET @theSql=CONCAT(@theSql,' as total_amount from job_prices jp join kids_ages ka on ka.id = jp.kids_age_id'); 
    SET @theSql=CONCAT(@theSql,' where ka.age in(',age,') and ' ,in_hours, ' between jp.start_hours and jp.end_hours ');  
    PREPARE stmt from @theSql; -- create a prepared stmt from the above concat 
    EXECUTE stmt; -- run it 
    DEALLOCATE PREPARE stmt; -- cleanup 
END