MySQL where 子句定位依赖子查询

标签 mysql sql entity-attribute-value

我使用实体-属性-值数据库模型。

我的产品有很多 attribute_varchars 而 attribute_varchars 有一个属性。一个属性有多个 attribute_varchars,一个属性 varchar 有一个产品。相同的逻辑适用于 attribute_decimals 和 attribute_texts。

无论如何,我有以下查询,我想使用 where 子句过滤结果

SELECT 
    products.id,
    (select value from attribute_texts    where product_id = products.id and attribute_id = 1) 
        as description,
    (select value from attribute_varchars where product_id = products.id and attribute_id = 2) 
        as model,
    (select value from attribute_decimals where product_id = products.id and attribute_id = 9) 
        as rate,
    (select value from attribute_varchars where product_id = products.id and attribute_id = 20) 
        as batch
FROM products
WHERE products.status_id <> 5

我想添加一个 where rate > 5

我试过了,但出现以下错误:“where 子句”中的未知列“rate”。我尝试向值和值表添加别名,但似乎没有任何效果。

最佳答案

在 MySQL 中,您可以:

having rate > 5

MySQL 扩展了 having 子句,因此它可以在没有 group by 的情况下工作。虽然作为一项功能值得怀疑,但它确实允许您在 select 子句中引用别名,而无需使用子查询。

关于MySQL where 子句定位依赖子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793306/

相关文章:

sql - SQL 紧凑上的嵌套选择

mysql - 一次选择的两个独特插入物

mysql - GROUP BY 子句但在列中具有连续(线性)值

mysql - 同一属性 : null entries, EAV 存在多种可能的数据类型,还是存储为 varchar?

sql - 这个表模式的正确名称是什么?

SQL UPDATE 带有附加字符串 CONCAT 的字段中的所有值不起作用

php - php session 变量被覆盖

php - Magento -> 我希望管理员中的产品网格显示在前端

mysql - 将多行中的日期聚合为一行

mysql:从客户端向服务器导入大量行