涉及具有“fieldname”和“fieldvalue”列的表的SQL连接

时间:2016-06-02 21:16:30

标签: mysql sql

对不起,我在试图简化这个问题的时候完全搞砸了原来的问题。

我有一张拍卖网站的表格。我需要从“listing”表中返回与地理相关的搜索条件匹配的记录,这些记录将按如下方式传递:

province_id = 2
district_id = 1,4
suburb_id = 5,6

请注意,此逗号转化来自搜索表单中的复选框组,用户可以在其中选择多个省,郊区和/或地区进行搜索。

    record_id   title       
    ----------------------
    2231        Ford Mustang
    2232        Nissan GTR
    2233        Chev Camaro
    2234        Plymouth Roadrunner
    2235        Dodge Charger

listing_special_fields (与listing_id = listings.record_id上的列表相关)

    record_id   listing_id   fieldname      fieldvalue
    -----------------------------------------------
    1231        2232         province_id    2
    1232        2232         district_id    4
    1233        2232         suburb_id      1
    1234        2233         province_id    2
    1235        2233         district_id    4
    1236        2233         suburb_id      6
    1237        2234         province_id    2
    1238        2234         district_id    4
    1239        2234         suburb_id      5    

    record_id     province
    ----------------------
    1             Northland
    2             Auckland
    3             Waikato  

  (与province_id = provinces.record_id上的省相关)

    record_id     province_id   district
    ------------------------------------
    4             2             Auckland City
    5             2             Franklin
    6             2             Hauraki Gulf Islands    

郊区   (与district_id = districts.record_id上的区域相关)

    record_id     district_id   suburb
    ------------------------------------
    1             4             Arch Hill
    5             4             Avondale
    6             4             Balmoral

根据给定的搜索参数,我应该得到以下输出:

        record_id   title       
        ----------------------
        2233        Chev Camaro
        2234        Plymouth Roadrunner

查询应包含省,区和郊区表的连接。通常,如果listing_specials_fields表具有名为“province_id”,“district_id”和“suburb_id”而不是“fieldname”和“fieldvalue”的列,则会很简单。

1 个答案:

答案 0 :(得分:1)

您可以使用PIVOT来获得预期结果。以下是SQL Server的查询: -

<强>查询

Select Listings.record_id, Listings.title FROM 
(
    select ListingId, fieldId, fieldValue  FROM listing_special_fields
) AS BQ
PIVOT
(  
    MAX(fieldValue)
    FOR fieldId IN ([province_id], [district_id], [suburb_id])
) PV
INNER JOIN Listings ON Listings.record_id = PV.ListingId
WHERE PV.province_id IN (2)
    AND PV.district_id IN (1, 4)
    AND PV.suburb_id IN (5, 6)

<强>结果

order_id          title
  2233         Chev Camaro         
  2234         Plymouth Roadrunner       

希望,这对你有所帮助。

<强>更新

MySQL解决方案:

Select Listings.record_id, Listings.title from Listings
INNER JOIN
( Select  
    ListingId,
    MAX(
       case when fieldId = 'province_id' then fieldValue else null end
    ) AS province_id,
    MAX(
       case when fieldId = 'district_id' then fieldValue else null end 
    ) AS district_id,
    MAX(
       case when fieldId = 'suburb_id' then fieldValue else null end
    ) AS suburb_id
  from listing_special_fields
  group by ListingId
) AS PV ON PV.ListingId = Listings.record_id
WHERE PV.province_id IN (2)
   AND PV.district_id IN (1, 4)
   AND PV.suburb_id IN (5, 6)