我真的不确定如何解释这个,因为我刚开始尝试以更先进的方式学习如何使用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,我希望它将let的1762的值拉到旁边的列中。
因此,第1列将具有“And an.attributeid = 113319”拉出的所有值,第2列将具有“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中作为两个单独的结果返回的。我希望这两个结果都是一个结果,而是两列,而不是两个结果,每个结果都有一列。
如您所见,除了最后一行之外,每个选择中的所有行都相同。
我还觉得有一种更好的方法,就是必须使用整个代码两次才能获得an.attributeid的另一列。无论如何,我会拿走我能得到的东西。
如果你有任何建议,我会非常感激。
答案 0 :(得分:1)
你可以做这样的事情
... AND an.attributeid IN (113319, 1762 )
这将返回两行,一列。
如果你想将行转换成列,那就更复杂了。
答案 1 :(得分: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表。
答案 2 :(得分: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