繁体   English   中英

MySQL的:在子查询中使用派生表

[英]Mysql : Using derived table in subquery

在我的模型(非常简化)中,我有3个表:offers,attributes和attributes_offers。

我想在此查询中使用派生表中的子查询:

SELECT
    o.offer_id,
    GROUP_CONCAT(CONCAT(attr.attribute_code,'-',attr.value_id,'-',attr.value_value) SEPARATOR ';') AS attributes,
    (SELECT attr.value_id FROM attr WHERE attribute_code = 'area') AS area_id,
    (SELECT attr.value_id FROM attr WHERE attribute_code = 'category') AS category_id
FROM   offers o
INNER JOIN (
    SELECT offer_id,attribute_code,value_id,value_value
    FROM   attributes_offers ao
    INNER JOIN attributes att USING (attribute_code) 
) AS attr ON attr.offer_id = o.offer_id

但是MySql回答了我: Table 'attr' doesn't exist

Group_concat运作良好。

有没有办法在select子查询中使用我的派生表attr?

这是正常现象,因为表“ attr”和您要在其上使用的选择位于不同的块中...以这种方式查看,您要执行的选择在一个框中,您看不到外部内容。 尝试这个

SELECT
    o.offer_id,
    GROUP_CONCAT(CONCAT(attr.attribute_code,'-',attr.value_id,'-',attr.value_value) SEPARATOR ';') AS attributes,
   attr.value_id AS area_id,
    attr.value_id AS category_id
FROM   offers o
INNER JOIN (
    SELECT offer_id,attribute_code,value_id,value_value
    FROM   attributes_offers ao
    INNER JOIN attributes att USING (attribute_code) 
) AS attr ON attr.offer_id = o.offer_id

如果您要获取所有value_id,请尝试在另一个查询中执行此操作,因为您尝试执行的操作根本无法正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM