兄弟们,请教个sql语句
有这么一个 posts 表#
id | name | parent_id |
---|---|---|
1 | 公告 | 0 |
2 | 更新说明 | 1 |
3 | 帮助中心 | 0 |
然后我要根据父亲 name,查询出父亲本身以及其下一级的 id。比如我想查公告和公告下一级的 id,在上面例子来说,也就是根据 name = 公告的已知条件,查出 id=1 和 id=2 这两条记录,有什么效率高点的 sql 写法吗。#
我目前想到的写法是这样的,但是我感觉这样效率好像太低了
select * from posts where parend_id = (select id from posts where name='公告' ) union (select * from posts where name='公告' )
推荐文章: