2016-08-02 38 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