Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql:在子查询中使用派生表_Mysql - Fatal编程技术网

Mysql:在子查询中使用派生表

Mysql:在子查询中使用派生表,mysql,Mysql,在我的(非常简化的)模式中,我有3个表:offers、attributes、attributes\u 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

在我的(非常简化的)模式中,我有3个表:offers、attributes、attributes\u 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回答我:
表'attr'不存在

康卡特小组运作良好


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

这是正常的,因为表“attr”和您试图在其上使用的select位于不同的块中。。。这样看,你要做的选择是在一个盒子里,你看不到外面有什么。试试这个

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

如果您想获取所有值,请尝试在另一个查询中执行此操作,因为您尝试执行的查询根本不起作用

请为table
attr
使用不同的别名。并将别名
attr
更改为内部联接查询的任何其他名称。实际上,这就是我要做的,根据属性\u代码,在不同的列中获取一些值\u id。这样做的好方法是什么?您可以始终添加“WHERE”块并在属性代码上添加条件。请记住,下次在数据库中查询每行中的一行或多行时,属性有一个值,除非您使用另一个主题的对象数据库,否则无法获得多个值。