MySQL查询外键引用主键

时间:2022-01-25 21:44:13

I have this table that is referencing itself.

我有这个表正在引用它自己。

MySQL查询外键引用主键

Is there a way to show the parent name in a query? Something like if (parent_id != null) {SHOW name WHERE id=parent_id} else {is_parent = TRUE). This is just how I see the logic.

是否有方法在查询中显示父名称?类似if (parent_id != null){显示id=parent_id} else {is_parent = TRUE的名称。这就是我的逻辑。

1 个解决方案

#1


3  

Maybe simply:

也许简单:

SELECT U.*, UP.name
FROM units U
LEFT JOIN units UP ON U.parent_id = UP.id

?

吗?

#1


3  

Maybe simply:

也许简单:

SELECT U.*, UP.name
FROM units U
LEFT JOIN units UP ON U.parent_id = UP.id

?

吗?