2016-01-20 108 views
0

当我有两个单独的查询时。mySQL,将字段设置为另一个字段的值

Select `entity_id`,`parent_id`,`name`,`meta_description` 
    from catalog_category_flat_store_1 
    where level = 3 
    and is_active = 1 
    order by parent_id asc, position asc 

,给了我这样的输出:

+-----------+-----------+------------+----------+ 
| entity_id | parent_id | Name  | desc  | 
+-----------+-----------+------------+----------+ 
|  1 |  0 |  test | text  | 
+-----------+-----------+------------+----------+ 

我有这样一个单独的查询,

Select `entity_id`,`parent_id`,`name`,`meta_description` 
     from catalog_category_flat_store_1 
     where level = 2 
     and is_active = 1 
     order by parent_id asc, position asc 

,给了我这样的输出:

+-----------+-----------+------------+----------+ 
    | entity_id | parent_id | Name  | desc  | 
    +-----------+-----------+------------+----------+ 
    |  2 |  1 |  test2 | text  | 
    +-----------+-----------+------------+----------+ 

我需要结合他们Ë两个查询,从而使输出看起来像这样:

+-----------+-----------+------------+----------+ 
    | entity_id | parent_nm | Name  | desc  | 
    +-----------+-----------+------------+----------+ 
    |  2 |  test |  test2 | text  | 
    +-----------+-----------+------------+----------+ 

我试图创建一个子查询做到这一点,但我有没有快乐,任何人都可以提出查询可能如何构建。谢谢

回答

0

这是做你想做的? 不是子查询,而是将表加入自己 - 您需要使用别名来区分表的两个实例。您可能需要修改“order by”以获得您所需的订购 - 因为您的示例只有1个结果,我猜测。

Select a.`entity_id`,b.`Name` as `parent_nm`,a.`name`,a.`meta_description` 
from catalog_category_flat_store_1 a 
join catalog_category_flat_store_1 b on a.`parent_id`=b.`entity_id` and b.`level`=3 and b.`is_active`=1 
where a.level = 2 
and a.is_active = 1 
order by a.parent_id asc, b.parent_id asc, a.position asc, b.position asc 
相关问题