搜尋

首頁  >  問答  >  主體

如何在MySQL中將2個procuderes聚合為1個?

我有兩個看起來幾乎相同的程式。一個人收到位置價格並進行操作,另一個人收到體驗價格。 < /p>

第一個:

-- Returns: service providers in given location and price
DELIMITER &&  
CREATE PROCEDURE get_service_providers_location_price (IN id_location INT,IN price DOUBLE,IN limite INT, IN inicio INT)  
BEGIN  
    SELECT user.idUser, user.name,user.lastActivity,user.active,serviceprovider.description,location.name AS location, location.cordsX, location.cordsY, file.image FROM user
    INNER JOIN location ON user.idLocation = location.idLocation
    INNER JOIN file ON user.idUser = file.idUser
    INNER JOIN serviceprovider ON user.idUser = serviceprovider.idSP 
    INNER JOIN category_has_serviceprovider ON serviceprovider.idSP = category_has_serviceprovider.idServiceProvider 
        WHERE user.type = 3 AND user.idLocation = id_location  
            AND (category_has_serviceprovider.price <= price OR category_has_serviceprovider.price IS NULL) and serviceprovider.idSubscription != 1 
    ORDER BY CASE WHEN serviceprovider.idSubscription in (5,6,7) then 1 else 2 end, serviceprovider.endSub ASC LIMIT limite OFFSET inicio;
END &&  
DELIMITER ;

第二個:

-- Returns: service providers in given experience and price
DELIMITER &&  
CREATE PROCEDURE get_service_providers_experience_price (IN experience INT, IN price DOUBLE,IN limite INT, IN inicio INT)  
BEGIN  
    SELECT user.idUser, user.name,user.lastActivity,user.active,serviceprovider.description,location.name AS location, location.cordsX, location.cordsY, file.image FROM user
    INNER JOIN location ON user.idLocation = location.idLocation
    INNER JOIN file ON user.idUser = file.idUser
    INNER JOIN serviceprovider ON user.idUser = serviceprovider.idSP 
    INNER JOIN category_has_serviceprovider ON serviceprovider.idSP = category_has_serviceprovider.idServiceProvider 
        WHERE user.type = 3
            AND (category_has_serviceprovider.price <= price OR category_has_serviceprovider.price IS NULL) AND category_has_serviceprovider.experience >= experience and serviceprovider.idSubscription != 1 
    ORDER BY CASE WHEN serviceprovider.idSubscription in (5,6,7) then 1 else 2 end, serviceprovider.endSub ASC LIMIT limite OFFSET inicio;
END &&  
DELIMITER ;

如您所見,只有 WHERE 子句發生了變化。在 MySQL 中,是否可以將這兩個過程聚合為一個?因為我有大約 5 個看起來相同的程序,但它只是改變了 WHERE 子句,我發現為每種情況執行單獨的過程很煩人。

P粉998920744P粉998920744308 天前399

全部回覆(2)我來回復

  • P粉315680565

    P粉3156805652024-01-30 00:22:32

    您可以使用 IFNULL。傳遞 experienceid_location 值並使用 NULL 作為另一個值。

    為參數制定命名方案(此處為 in_-prefix)也是一個好習慣,以便參數與列名稱不同。

    DELIMITER &&  
    CREATE PROCEDURE get_service_providers_experience_price (
    in_experience INT, 
    in_id_location INT, 
    in_price DOUBLE,
    in_limite INT, 
    in_inicio INT
    )  
    BEGIN  
    SELECT 
      user.idUser, 
      user.name,
      user.lastActivity,
      user.active,
      serviceprovider.description,
      location.name AS location, 
      location.cordsX, 
      location.cordsY, 
      file.image 
    FROM user
      INNER JOIN location ON user.idLocation = location.idLocation
      INNER JOIN file ON user.idUser = file.idUser
      INNER JOIN serviceprovider ON user.idUser = serviceprovider.idSP 
      INNER JOIN category_has_serviceprovider ON serviceprovider.idSP = category_has_serviceprovider.idServiceProvider 
    WHERE user.type = 3
      AND (category_has_serviceprovider.price <= in_price OR category_has_serviceprovider.price IS NULL) 
      AND category_has_serviceprovider.experience >= IFNULL(in_experience, category_has_serviceprovider.experience)
      AND user.idLocation = IFNULL(id_location, user.idLocation)
      AND serviceprovider.idSubscription != 1 
    ORDER BY CASE WHEN serviceprovider.idSubscription in (5,6,7) then 1 else 2 end, serviceprovider.endSub ASC 
    LIMIT in_limite OFFSET in_inicio;
    
    END 
    &&  
    DELIMITER ;

    回覆
    0
  • P粉951914381

    P粉9519143812024-01-30 00:22:16

    例如,您可以使用這個:

    CREATE PROCEDURE get_service_providers_price (IN experience INT,IN id_location INT,IN price DOUBLE,IN limite INT, IN inicio INT)  
    BEGIN  
    SELECT user.idUser, user.name,user.lastActivity,user.active,serviceprovider.description,location.name AS location, location.cordsX, location.cordsY, file.image FROM user
    INNER JOIN location ON user.idLocation = location.idLocation
    INNER JOIN file ON user.idUser = file.idUser
    INNER JOIN serviceprovider ON user.idUser = serviceprovider.idSP 
    INNER JOIN category_has_serviceprovider ON serviceprovider.idSP = category_has_serviceprovider.idServiceProvider 
    WHERE user.type = 3 
      AND (category_has_serviceprovider.price <= price OR category_has_serviceprovider.price IS NULL) 
      and serviceprovider.idSubscription != 1 
      AND CASE WHEN experience IS NOT NULL
               THEN category_has_serviceprovider.experience >= experience
               ELSE user.idLocation = id_location  
               END
    ORDER BY CASE WHEN serviceprovider.idSubscription in (5,6,7) then 1 else 2 end, serviceprovider.endSub ASC LIMIT limite OFFSET inicio;
    

    如果提供 IN 經驗 INT 設定為某個值,則套用它的條件。如果您為此參數提供 NULL,則套用 IN id_location INT 的條件。

    注意 - 現在你的 SP 有 5 個參數,而不是 4 個。

    PS。您的 SP 包含一條 SQL 語句 - 因此不需要 BEGIN-END 和 DELIMITER。

    PPS。使用類似的方法,您可以建立 SP,它可以同時套用其中一個條件,也可以同時套用這兩個條件,或者都不套用。例如,它可以是:

    AND CASE WHEN experience IS NOT NULL AND id_location IS NOT NULL  -- apply both parameters filtering
             THEN category_has_serviceprovider.experience >= experience AND user.idLocation = id_location
             WHEN experience IS NOT NULL   -- apply filtering by experience  only
             THEN category_has_serviceprovider.experience >= experience
             WHEN id_location IS NOT NULL   -- apply filtering by location only
             THEN user.idLocation = id_location
             ELSE 1    -- not filter, return all rows
             END
    

    購買力平價。如果您想要有 2 個獨立的函數但有一份程式碼副本(例如,這些函數名稱已在一堆程式碼中使用),那麼您可以這樣做:

    CREATE PROCEDURE get_service_providers_location_price (IN id_location INT,IN price DOUBLE,IN limite INT, IN inicio INT)
    CALL get_service_providers_price (NULL, id_location, price, limite, inicio);
    
    CREATE PROCEDURE get_service_providers_experience_price (IN experience INT, IN price DOUBLE,IN limite INT, IN inicio INT)
    CALL get_service_providers_price (experience, NULL, price, limite, inicio);
    

    回覆
    0
  • 取消回覆