mySQL,将一个字段设置为另一个字段的值

mySQL,将一个字段设置为另一个字段的值,mysql,subquery,Mysql,Subquery,A现在我有两个不同的问题 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 这给了我这样一个输出: +-----------+-----------+------------+----------+ |

A现在我有两个不同的问题

 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     |
+-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_id | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      1    |      test2 | text     |
    +-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_nm | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      test |      test2 | 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     |
+-----------+-----------+------------+----------+
|       1   |      0    |      test  | text     |
+-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_id | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      1    |      test2 | text     |
    +-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_nm | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      test |      test2 | text     |
    +-----------+-----------+------------+----------+
我需要组合这两个查询,以便输出如下所示:

+-----------+-----------+------------+----------+
| entity_id | parent_id | Name       | desc     |
+-----------+-----------+------------+----------+
|       1   |      0    |      test  | text     |
+-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_id | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      1    |      test2 | text     |
    +-----------+-----------+------------+----------+
    +-----------+-----------+------------+----------+
    | entity_id | parent_nm | Name       | desc     |
    +-----------+-----------+------------+----------+
    |       2   |      test |      test2 | text     |
    +-----------+-----------+------------+----------+

我试图创建一个子查询来实现这一点,但我没有兴趣,有人能建议如何构造查询吗。谢谢

这是你想要的吗? 与子查询不同的是,将表连接到自身-您需要使用别名来区分表的两个实例。您可能需要修改“orderby”以获得所需的排序—我猜您的示例只有一个结果

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