mysql - 在 MySQL 中为多个属性添加列

标签 mysql join attributes key-value

我真的不确定如何解释这一点,因为我今天才开始尝试学习如何以更高级的方式使用 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 的值拉到它旁边的一列中。

因此,第 1 列将包含“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 中作为两个单独的结果返回。我希望这两个结果都在一个结果中,但作为两列,而不是两个结果各一列。

如您所见,除了最后一行之外,每个选择中的所有行都是相同的。

我还觉得有一种更好的方法,而不是为了获得 an.attributeid 的另一列而不得不使用整个代码两次。不管怎样,我会得到我能得到的。

如果您有任何建议,我将不胜感激。

最佳答案

如果您真的想这样做,用于格式化或报告目的。这是您会使用的 hack。

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

关于mysql - 在 MySQL 中为多个属性添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088155/

相关文章:

MySQL 到 Postgres 按错误分组

html - 在发送到 sql 服务器之前从 html 输入元素中提取值

postgresql - 从 PostgreSQL 中的另一个表中获取列值

Python:对象没有metadata_parser 0.6.6的属性

mysql - Knex.js 使用别名计算不同的列

php - PHP mysql 查询中的多个ANDS?

MySQL : selecting minimum value from one table and country details from another and grouping them according to country code

php - MySQL 加入两个表给出了不正确的结果

asp.net-mvc - ASP.NET MVC RequireHttps 不可用

javascript - d3 改变所有元素的属性