MySql:FIND_IN_SET无法正常工作

时间:2013-06-14 07:09:16

标签: mysql sql database

我有一个包含字符串参数的存储过程。此参数与'759,760,761'分开。在where子句中,我想像这样使用它。

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

但是没有返回的数据,但是有759县id的行。如果删除此子句,数据就会出现。

这是我的所有SQL代码:

    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;

1 个答案:

答案 0 :(得分:4)

尝试这种方式:

...AND FIND_IN_SET(REL.CountyId, '759,760,761') > 0
查询中的

可能如下所示:

...
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)

  

如果字符串str在包含N个子串的字符串列表strlist中,则返回1到N范围内的值。

     

如果str不在strlist中,或者strlist是空字符串,则返回0。

...所以,大于0的数字意味着“找到”。

除非您对列表中的特定位置感兴趣,否则in部分在这种情况下无用。