mysql - 两列,相同的数据,不同的Where子句

标签 mysql sql magento

我需要使用两个不同的 WHERE 子句语句查询同一列。

我正在使用 Magento,但无法更改表格格式。

表格 View :catalog_product_entity_varchar

entity_id | attribute_id | value                |
5         | 60           | Bear Apprentice      |
5         | 86           | bear-apprentice      |
5         | 71           | Item Description     |
5         | 74           | /a/p/apprentice2.jpg |

我希望将其显示为:

entity_id | Item Name            |  img                   |
5         |  Bear Apprentice     | /a/p/apprentice2.jpg   |

最终使 attribute_id 6074 出现在同一行但在两个单独的列中。 是否可以对列别名执行 WHERE 子句?

谢谢!

最佳答案

在 mySQL 中,透视的一种方法是使用 casemax 以及 group by。这确实假设一个entity_Id只能有一个配对值(每个实体每个值1个属性),例如60 对于实体​​ 5 只能出现一次。

SELECT entity_ID
     , max(case when attribute_Id = 60 then value end) as `Item Name`
     , max(case when attribute_Id = 74 then value end) as img
FROM tableName
WHERE entity_ID = 5
GROUP BY entity_ID

关于mysql - 两列,相同的数据,不同的Where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727958/

相关文章:

mysql - 我可以使用 Electron JS 将我的 Web 应用程序转换为桌面应用程序吗

MySQL - 选择除 1 以外的所有不同行

python - Django:异常值(2013, '2013: Lost connection to MySQL server during query',无)

mysql - 如何使用mysql replace将所有双引号替换为单引号?

php - 如何从 magento 电子商务中获取特定类别的产品

php - 我们可以在 magento 中进行小部件选项验证吗?

mysql - 新的 SQL 安装已经将我锁定为 root

sql服务器: include column that is not part of an aggregate function or Group by clause

sql - 将 sql 整数舍入到最接近的小时

zend-framework - 如何对可配置产品使用分级定价? (Magento 1.4+)