-3

このコードを実行すると、mysql は許可されていないという構文エラーを表示します

DELIMITER //
    CREATE PROCEDURE `GetHotelByLocationArrivalDeparture`(IN strLocation VARCHAR(500),IN dateArrival DATE,IN dateDeparture DATE,IN strOrderBy VARCHAR(500),IN nMinPrice FLOAT,IN nMaxPrice FLOAT)
BEGIN 

SELECT
        HT.HotelId AS HotelId,
        HT.HotelName AS HotelName,
        HT.HotelDescription AS HotelDescription,
        HT.HotelLogo AS HotelLogo,
        HT.HotelStar AS HotelStar,
        HT.HotelLocation AS HotelLocation,
        HT.HotelLatitude AS HotelLatitude,  
        HT.HotelLongitude AS HotelLongitude,
        HT.AccomodationTypeId AS AccomodationTypeId,
        RM.RoomId AS RoomId,
        RM.RoomName AS RoomName,
        RM.RoomMaxPeople AS RoomMaxPeople,
        RM.RoomCurrency AS RoomCurrency,
        RM.RoomMinDays AS RoomMinDays,
        RM.RoomKeyInfo AS RoomKeyInfo,
        RM.RoomBedTypeOneId AS RoomBedTypeOneId,
        RM.RoomBedTypeTwoId AS RoomBedTypeTwoId,
        DR.DateRangeId AS DateRangeId,
        DR.DateRangeFromDate AS DateRangeFromDate,
        DR.DateRangeToDate AS DateRangeToDate,
        DR.DateRangeNote AS DateRangeNote,
        DR.CurrencyId AS DateRangeCurrencyId,
        DR.DateRangeMinNight AS DateRangeMinNight,
        DR.RoomId AS DateRangeRoomId,
        DRR.DateRowId AS DateRowId,
        DRR.DateRowNetRate AS DateRowNetRate,
        DRR.DateRowDiscount AS DateRowDiscount,
        DRR.DateRowDiscountedNet AS DateRowDiscountedNet,
        DRR.DateRowTax AS DateRowTax,
        DRR.DateRowHotelPrice AS DateRowHotelPrice,
        DRR.DateRowMarkup AS DateRowMarkup,
        DRR.DateRowClientPrice AS DateRowClientPrice,
        DRR.DateRowConvertedPrice AS DateRowConvertedPrice,
        DRR.DateRowType AS DateRowType
    FROM 
        tblhotel HT,
        tblroom RM,
        tbldaterange DR,
        tbldaterow DRR
WHERE 
        (
                LOWER(HT.HotelName) REGEXP TRIM(BOTH '"' FROM strLocation) 
             OR LOWER(HT.HotelLocation) REGEXP TRIM(BOTH '"' FROM strLocation)
        )  
    AND 
        (
            (
                    DR.DateRangeToDate > TRIM(BOTH '"' FROM dateArrival) 
                AND TRIM(BOTH '"' FROM dateDeparture) >  DR.DateRangeFromDate 
                AND TRIM(BOTH '"' FROM dateArrival)   >= DR.DateRangeFromDate
                AND TRIM(BOTH '"' FROM dateDeparture) <= DR.DateRangeToDate
            )
            OR
            (
                ( 
                    TRIM(BOTH '"' FROM dateDeparture) BETWEEN DR.DateRangeFromDate AND DR.DateRangeToDate
                ) 
                AND TRIM(BOTH '"' FROM dateDeparture) >  DR.DateRangeFromDate 
                AND TRIM(BOTH '"' FROM dateArrival)   >= DR.DateRangeFromDate
                AND TRIM(BOTH '"' FROM dateDeparture) <= DR.DateRangeToDate
            )
        )
    AND (HT.HotelId     = RM.HotelId)
    AND (RM.RoomId      = DR.RoomId)
    AND (DR.DateRangeId = DRR.DateRangeId)    
    AND 
        (       HT.IsActive = 1 
            AND HT.IsDeleted <> 1 
            AND RM.IsActive = 1 
            AND RM.IsDeleted <> 1 
            AND DR.IsActive = 1 
            AND DR.IsDeleted <> 1
        )
        /* This is what i want to do */
        IF (nMinPrice > AND nMaxPrice > 0) THEN
            AND DRR.DateRowClientPrice BETWEEN nMinPrice AND nMaxPrice
        END IF;    
        /* This is what i want to do */
        ORDER BY 
                CASE WHEN strOrderBy='sorthotelname_DESC' THEN HT.HotelName END DESC,  
                CASE WHEN strOrderBy='sorthotelname_ASC' THEN HT.HotelName END ASC,
                CASE WHEN strOrderBy='sorthotelstars_DESC' THEN HT.HotelStar END DESC,  
                CASE WHEN strOrderBy='sorthotelstars_ASC' THEN HT.HotelStar END ASC,
                CASE WHEN strOrderBy='sorthotelprice_DESC' THEN CONCAT(DRR.DateRowClientPrice ,'') END DESC,  
                CASE WHEN strOrderBy='sorthotelprice_ASC' THEN CONCAT(DRR.DateRowClientPrice ,'') END ASC,
                CASE WHEN strOrderBy='sorthotellocation_DESC' THEN HT.HotelLocation END DESC,  
                CASE WHEN strOrderBy='sorthotellocation_ASC' THEN HT.HotelLocation END ASC;
END //
DELIMITER ;
4

1 に答える 1

1

IFそのような WHERE 句の途中に を投げることはできないと思います。IF代わりに、おそらく次のようなブール式になるようにロジックを作り直してください。

and case
    when nMinPrice > 0 and nMaxPrice > 0 then
        DRR.DateRowClientPrice BETWEEN nMinPrice AND nMaxPrice
    else
        1
    end

nMinPricenMaxPriceが正の場合、その CASE の値は次のようになります。

DRR.DateRowClientPrice BETWEEN nMinPrice AND nMaxPrice

nMinPriceまたは のいずれかが正でない場合nMaxPrice、その CASE は1(つまり真) になり、TRUE との論理積はノーオペレーションであるため、CASE は事実上無視されます。

于 2013-08-03T05:29:32.493 に答える