mysql数据表TABLE_A中有字段id,parent_id,写出可以通过id查询数据层级的sql
时间: 2024-06-02 20:13:17 浏览: 139
MySQL通过自定义函数实现递归查询父级ID或者子级ID
SELECT t1.*, t2.parent_id as parent_parent_id, t3.parent_id as parent_parent_parent_id
FROM TABLE_A t1
LEFT JOIN TABLE_A t2 ON t1.parent_id = t2.id
LEFT JOIN TABLE_A t3 ON t2.parent_id = t3.id
WHERE t1.id = [输入的id];
阅读全文