1

我已经尝试了各种类似的解决方案,我在网上找到了这些解决方案,但到目前为止,似乎没有什么能完全符合我的需要。

鉴于下表,为了这个问题的目的,大大简化了:

tblRetailers

retailer_id    company_name
---------------------------
1              ABC Ltd
2              DEF Ltd

tblRetailerFeature

retailer_feature_id   retailer_id   feature_option_id
-----------------------------------------------------
1                     1             1
2                     1             4
3                     2             3
4                     2             6

tblFeatureOptions

feature_option_id   feature_id   title     Price
------------------------------------------------
1                   1            30 Days   14.95
2                   1            60 Days   29.90
3                   1            90 Days   44.85
4                   2            30 Days   19.95
5                   2            60 Days   39.90
6                   2            90 Days   59.95

tblFeatures

feature_id   title   
----------------------------
1            Top Listing
2            Gallery Listing
3            Premium Listing

我需要确定每个零售商是否有“顶级”、“画廊”和/或“高级”列表

因此,最终结果应如下所示:

retailer_id   hasTopListing   hasGalleryListing   hasPremiumListing
--------------------------------------------------------------------
1             0               1                   0
2             1               1                   0

0/1 是布尔值。如何为此编写 SQL?

4

2 回答 2

2

我看不到您的架构是如何工作的,因为我无法从您提供的输入中解释您期望的输出。

不过,这可能会有所帮助:

SELECT
  r.retailer_id,
  MAX(fo.feature_id = 1) AS hasTopListing,
  MAX(fo.feature_id = 2) AS hasGalleryListing,
  MAX(fo.feature_id = 3) AS hasPremiumListing
FROM         tblFeatureOptions fo
  INNER JOIN tblRetailerFeature rf
          ON rf.feature_option_id = fo.feature_option_id
  RIGHT JOIN tblRetailers r
          ON r.retailer_id = rf.retailer_id
GROUP BY r.retailer_id

也可在http://sqlfiddle.com/#!2/ca9f8/10上获取您的示例数据

我假设您tblRetailerFeature加入tblFeatureOptions使用feature_option_id. 然后,我只需检查给定零售商 ID 的任何行是否提到了每个相关的功能 ID。MySQL 已经将比较视为 0/1 值,因此MAX此处对应于OR所有行。

正如您从小提琴中可以看出的那样,输出不是您所要求的,但这是我对您输入的期望。所以要么指出你的数据是如何被连接的,要么自己调整它。

于 2013-07-29T09:37:23.053 回答
1

好的,这是您的解决方案:DEMO

SELECT r.retailer_id,
       IF(rf_1.feature_option_id > 0, 1, 0) AS hasTopListing,
       IF(rf_2.feature_option_id > 0, 1, 0) AS hasGalleryListing,
       IF(rf_3.feature_option_id > 0, 1, 0) AS hasPremiumListing
FROM tblRetailers r
LEFT JOIN tblRetailerFeature rf_1 ON r.retailer_id = rf_1.retailer_id
AND rf_1.feature_option_id IN
  ( SELECT feature_option_id
   FROM tblFeatureOptions
   WHERE feature_id = 1)
LEFT JOIN tblRetailerFeature rf_2 ON r.retailer_id = rf_2.retailer_id
AND rf_2.feature_option_id IN
  ( SELECT feature_option_id
   FROM tblFeatureOptions
   WHERE feature_id = 2)
LEFT JOIN tblRetailerFeature rf_3 ON r.retailer_id = rf_3.retailer_id
AND rf_3.feature_option_id IN
  ( SELECT feature_option_id
   FROM tblFeatureOptions
   WHERE feature_id = 3)

结果:

RETAILER_ID HASTOPLISTING   HASGALLERYLISTING   HASPREMIUMLISTING
1   1   1   0
2   1   1   0
于 2013-07-29T09:35:04.660 回答