在MySQL中为多个属性添加列

3

我真的不确定如何解释这个,因为我今天刚开始尝试学习如何更高级地使用MySQL查询。以下是我的代码:

SELECT pa.displayvalue as Brand
FROM product p
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1
WHERE p.isactive = 1 AND p.categoryid = 4871
AND an.attributeid = 113319;

所以我有一个查询,我的目的只是为“and an.attributeid = 113319”添加另一列。我只想添加另一列,但是不使用113319,我想将1762的值提取到旁边的列中。
所以第一列会有“and an.attributeid = 113319”拉取的所有值,第二列会有“and an.attributeid = 1762”拉取的所有值。
我已经让这段代码工作了:
SELECT pa.displayvalue as Brand
FROM product p
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1
WHERE p.isactive = 1 AND p.categoryid = 4871
AND an.attributeid = 113319;

SELECT pa.displayvalue as Type
FROM product p
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1
WHERE p.isactive = 1 AND p.categoryid = 4871
AND an.attributeid = 1762;

但是在navicat中,这被作为两个单独的结果返回。我希望这两个结果都在一个结果中,但是作为两个列,而不是每个列只有一个结果。
正如您所看到的,所有这些Select中的行都是相同的,除了最后一行。
我还觉得有一种更好的方法来获取另一个属性ID列,而不必两次使用整个代码。无论如何,我会尽力而为。
如果您有任何建议,我将非常感激。

1
你所要求的有点复杂,99.999999999% 的时间不是一个好主意。行应该代表相关数据而不是页面上您绘制无关列的行。寻找那种输出时,最好在应用层处理它。 - Uueerdo
@Uueerdo 完全同意。我会增加两种在应用程序级别处理它的方法:进行两个查询并将结果并排显示,或者如果连接操作很耗费资源,则进行一个中间查询以生成包含两个结果的表格,并对该表格执行两个查询。 - qoba
所以我试图更改的这些数字,它们正在提取产品的属性。因此,一个数字将为一堆产品提取品牌名称,而另一个数字将提取所有品牌类型。我猜我想做的是制作一个CSV,列出品牌和类型。两者彼此相关。因此,如果这是Excel,则标题将是:“品牌”和“类型”,然后每行将填充每个产品的品牌和类型。 - DarkMatter
3个回答

1
你可以像这样做:

... 并且 an.attributeid IN (113319, 1762 )

这将返回两行一列的结果。

如果你想要将行转换为列,那就会变得更加复杂。


1
有时被称为键值存储。你需要使用多个连接来完成所需操作。这种方法可以解决问题。
SELECT whatever, 
       attr1.displayvalue attr1,
       attr2.displayvalue attr2
  FROM product p
  LEFT JOIN productattribute attr1 ON p.productid = attr1.productid
                                  AND attr1.attributeid = 113319
                                  AND attr1.localeid = 1
                                  AND attr1.isactive = 1
  LEFT JOIN productattribute attr2 ON p.productid = attr2.productid
                                  AND attr2.attributeid = 1762
                                  AND attr2.localeid = 1
                                  AND attr2.isactive = 1

你最终要将两个属性表拼接在一起,并从连接条件中选择适当的属性ID。使用左连接而不是普通连接,这样即使属性丢失,你仍然可以获取行。
这种数据组织方式在WordPress的wp_postmeta表中非常常见。

1
如果您真的想这样做,是为了格式化或报告目的。这是一个您会使用的技巧。
使用LEFT JOIN将返回左表中的所有行,以及右表中匹配的行。当没有匹配时,右侧的结果为NULL。
SELECT p.productid, t2.Type, t1.Brand, t3.Resolution 
FROM product p 
JOIN 
( 
    SELECT p.productid, pa.displayvalue as Brand 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 113319 
) t1 ON t1.productid = p.productid 
LEFT OUTER JOIN 
( 
    SELECT p.productid, pa.displayvalue as Type 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 1100 
) t2 ON t2.productid = p.productid 
LEFT OUTER JOIN 
( 
    SELECT p.productid, pa.displayvalue as Resolution 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 1762 
) t3 ON t3.productid = p.productid

这最终会创建100万+行数据,但实际上应该只有27行。 - DarkMatter
这里我使用了硬编码的键“1”,如果您只输出单行值。如果有多行,最好使用外键链接“JOIN”。我猜测您可以使用产品ID。 - ydoow
那就试一下吧,在 JOIN 查询中使用 product.[primary_key],看看结果是否符合你的预期。 - ydoow
非常抱歉我对这个主题的知识很少,但您能告诉我确切地在哪里放置它吗? - DarkMatter
1
让我们在聊天中继续这个讨论 - ydoow
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接