我有两个mysql表:
表1(80K +产品):
_products
product_id status ean category
表2:(700K +产品属性,在“ean”栏上的第一表上匹配)
_productinformation
info_id ean info_group info_type info_value
我的挑战:
我想从“产品”表中选择30个产品(根据其状态和类别),并将列ean上的这些产品与另一个表匹配。接下来,我想过滤掉“productinformation”表中的匹配项,其中ean与“products”中选择的ean相同,而“info_type”列匹配特定值。以下SQL几乎可以实现我想要的,除了两个问题:
我构建的查询:
SELECT _products.ean
, _products.status
, product_brand.info_value as product_brand
, product_type.info_value as product_type
, product_price.info_value as product_price
FROM _products
LEFT JOIN _productinformation ON _products.ean = _productinformation.ean
LEFT JOIN _productinformation as product_brand ON _products.ean = product_brand.ean
LEFT JOIN _productinformation as product_type ON _products.ean = product_type.ean
LEFT JOIN _productinformation as product_price ON _products.ean = product_price.ean
WHERE product_brand.info_type = 'brand'
AND product_type.info_type = 'type'
AND product_price.info_type = 'price'
AND _products.category='1'
返回类似(40多行,使用相同产品)的内容:
ean status product_brand product_type product_price 0123456789 1 brand1 type1 0.00 0123456789 1 brand1 type1 0.00 0123456789 1 brand1 type1 0.00 0123456789 1 brand1 type1 0.00 0123456789 1 brand1 type1 0.00 etc.
但是,我希望看到30种不同的产品:
ean status product_brand product_type product_price 0123456789 1 brand1 type1 0.00 9876543210 3 brand6 type3 15.00 6548214656 45 brand34 type1 99.00 245511411241 4 brand324 type1 98.00 etc.
有人可以告诉我,我正在寻找的查询是否可行?它应该怎么样?我已经尝试了100多个不同的查询(进一步3天......),但都失败了。上面的查询最接近。 希望有人能帮助我!日Thnx!
答案 0 :(得分:0)
我将在下面的表格中举例说明这个答案。它们与你的相似,只是我遗漏了一些似乎不会在这个查询中发挥作用的字段。
#Products
+ ---------- + ------ + ------------ +
| Product_Id | Status | EAN |
+ ---------- + ------ + ------------ +
| 1 | 1 | 0123456789 |
| 2 | 3 | 9876543210 |
| 3 | 45 | 6548214656 |
| 4 | 4 | 245511411241 |
+ ---------- + ------ + ------------ +
#Info
+ ------- + ------------ + --------- + ---------- +
| Info_Id | EAN | Info_Type | Info_Value |
+ ------- + ------------ + --------- + ---------- +
| 1 | 0123456789 | brand | brand1 |
| 2 | 0123456789 | type | type1 |
| 3 | 0123456789 | price | 0.00 |
| 4 | 9876543210 | brand | brand6 |
| 5 | 9876543210 | type | type3 |
| 6 | 9876543210 | price | 15.00 |
| 7 | 6548214656 | brand | brand34 |
| 8 | 6548214656 | type | type1 |
| 9 | 6548214656 | price | 99.00 |
| 10 | 245511411241 | brand | brand324 |
| 11 | 245511411241 | type | type1 |
| 12 | 245511411241 | price | 98.00 |
+ ------- + ------------ + --------- + ---------- +
现在考虑以下查询
select i.EAN
, p.ProductStatus
, case info_type when 'brand' then info_value end as brand
, case info_type when 'type' then info_value end as [type]
, case info_type when 'price' then info_value end as price
from #Info i
inner join #Products p on p.ean = i.ean
生成类似
的表格+ ------------ + ------ + -------- + ----- + ----- +
| EAN | status | brand | type | price |
+ ------------ + ------ + -------- + ----- + ----- +
| 0123456789 | 1 | brand1 | NULL | NULL |
| 0123456789 | 1 | NULL | type1 | NULL |
| 0123456789 | 1 | NULL | NULL | 0.00 |
| 9876543210 | 3 | brand6 | NULL | NULL |
| 9876543210 | 3 | NULL | type3 | NULL |
| 9876543210 | 3 | NULL | NULL | 15.00 |
| 6548214656 | 45 | brand34 | NULL | NULL |
| 6548214656 | 45 | NULL | type1 | NULL |
| 6548214656 | 45 | NULL | NULL | 99.00 |
| 245511411241 | 4 | brand324 | NULL | NULL |
| 245511411241 | 4 | NULL | type1 | NULL |
| 245511411241 | 4 | NULL | NULL | 98.00 |
+ ------------ + ------ + -------- + ----- + ----- +
正如您所看到的,我只在选择部分中使用单个连接和一些case语句来创建我想要的列。这比使用多个连接具有巨大的优势,因为没有任何记录重复。
要完成,简单的聚合将为我们提供我们想要的内容
select i.EAN
, p.ProductStatus
, max(case info_type when 'brand' then info_value end) as brand
, max(case info_type when 'type' then info_value end) as [type]
, max(case info_type when 'price' then info_value end) as price
from #Info i
inner join #Products p on p.ean = i.ean
group by i.EAN, p.ProductStatus
+ ------------ + ------ + -------- + ----- + ----- +
| EAN | Status | brand | type | price |
+ ------------ + ------ + -------- + ----- + ----- +
| 0123456789 | 1 | brand1 | type1 | 0.00 |
| 245511411241 | 4 | brand324 | type1 | 98.00 |
| 6548214656 | 45 | brand34 | type1 | 99.00 |
| 9876543210 | 3 | brand6 | type3 | 15.00 |
+ ------------ + ------ + -------- + ----- + ----- +
然后,您可以使用CTE过滤此表的特定字段,如下所示(仅限SQL Server ):
; with
CTE as (
select i.EAN
, p.ProductStatus
, max(case info_type when 'brand' then info_value end) as brand
, max(case info_type when 'type' then info_value end) as [type]
, max(case info_type when 'price' then info_value end) as price
from #Info i
inner join #Products p on p.ean = i.ean
group by i.EAN, p.ProductStatus
)
select *
from CTE
where price >= 99 -- or whatever other filter you want
希望这有帮助!