需要一个mysql查询 [英] Need a mysql query

查看:69
本文介绍了需要一个mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为角色"的表

I have a table named 'role'

id | parent_id | name
---+-----------+--------------------
1  | NULL      | Machine
2  | 3         | Ram
3  | 4         | Cpu
4  | NULL      | Computer
5  | NULL      | Electronic Device

我需要一个(mysql或postsql sql)查询,该查询将给出特定角色的可能父级.

I need a (mysql or postgress sql) query which will give the possible parents of a particular role.

例如

计算机"的可能的父角色是

the possible parent roles of 'Computer' are

id | name
---+-------------------
5  | Electronic Device
1  | Machine

请帮助我

推荐答案

使用:

SELECT t.id,
       t.name
  FROM ROLES t
 WHERE t.parent_id IS NULL
   AND t.name != ?

......,其中?是您要排除的name(如果其parent_id为null).

...where ? is whatever name you want to exclude if it's parent_id is null.

这篇关于需要一个mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆