确定用户是否具有各种高级功能

时间:2013-07-29 09:01:07

标签: mysql sql

我已尝试过各种类似的解决方案,我在网上找到了这个,但到目前为止似乎没有什么比我需要的更合适。

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

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?

2 个答案:

答案 0 :(得分: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加入tblFeatureOptionsfeature_option_id。然后我简单地检查给定零售商id的任何行是否提到每个相关的特征ID。 MySQL将比较视为0/1值,因此MAX对应于所有行的OR

正如你可以从小提琴那里得知的那样,输出并不是你要求的,但它是我对你输入的期望。因此要么指出你的数据如何加入,要么自己调整一下。

答案 1 :(得分: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