Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含字段的表id,pid,name。我想通过单个查询获取以下格式的数据。
id,pid,name
id- parent category name- name
名称将是类别或子类别,如果其父类别名称将是父类别名称,则父类别名称将为无。
尝试这个:
select t.id, tp.name as parent_category, t.name as category from table t full join table tp on tp.id = t.pid
您可以使用以下查询:
SELECT c.name,pc.name FROM category c left join category pc on c.pid = pc.id;