0

I have a stored procedure which has a string parameter. This parameter comes seperated, as '759,760,761'. In where clause I want to use it like this.

...AND REL.CountyId IN (FIND_IN_SET(REL.CountyId, '759,760,761') > 0) 

But there is no returned data however there is row with 759 county id. Datas come if I delete this clause.

Here is my all sql code:

    SELECT 
    REL.RealEstateListingId,
    (SELECT 
            ImageUrl
        FROM
            realestateimage
        WHERE
            IsMainImage = 1 AND Status = 1
                AND RealEstateListingId = REL.RealEstateListingId) AS ImageUrl,

    REL.ListingNumber,
    REL.Caption,
    REL.M2,
    REL.RoomCount,
    /*CONCAT((SELECT Name FROM City WHERE CityId IN (SELECT CityId FROM County WHERE CountyId = REL.CountyId)),'/', (SELECT Name FROM County WHERE CountyId = REL.CountyId)) AS Region,*/
    REL.Price,
    REL.Creation,
    REL.CountyId
FROM
    realestatelisting AS REL
        INNER JOIN
    User AS U ON U.UserId = REL.CreatedBy
        /*INNER JOIN realestatefilterrelation AS RFR ON RFR.RealEstateId = REL.RealEstateListingId*/
WHERE  REL.Price  BETWEEN 0 AND 1000000
   AND CAST(REL.M2 AS SIGNED) BETWEEN 0 AND 1000
   /*AND REL.CountyId IN (FIND_IN_SET(REL.CountyId, '759') > 0) 

  /*AND RFR.FilterContentId IN(FIND_IN_SET(RFR.FilterContentId, filterIds) > 0)*/
ORDER BY REL.Creation;
4

1 回答 1

4

Try this way:

...AND FIND_IN_SET(REL.CountyId, '759,760,761') > 0

in query might look like:

...
WHERE  REL.Price  BETWEEN 0 AND 1000000
   AND CAST(REL.M2 AS SIGNED) BETWEEN 0 AND 1000
   AND FIND_IN_SET(REL.CountyId, '759') > 0
   AND FIND_IN_SET(RFR.FilterContentId, filterIds) > 0
ORDER BY REL.Creation;

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings.

Returns 0 if str is not in strlist or if strlist is the empty string.

...so, number greater than 0 means "found".

Part with in is useless in this case unless you are interested in specific position in the list.

于 2013-06-14T07:24:12.030 回答