获取外键所指向的行 - mysql

时间:2018-04-06 17:04:02

标签: mysql select foreign-keys

现在我有了这个表redirectid

的内部外键
id  name  redirect
 1   a      null
 2   b      null
 3   c      null
 4   d       1
 5   e       3

我在开头只有name,我需要此查询=> If redirect is not null then get the new name from where redirect is pointing at.

例如

:如果我在开头有a,则不执行任何操作,因为重定向为空。但如果我在开始时d转到id = 1并获取a

1 个答案:

答案 0 :(得分:0)

使用自我加入:

SELECT t1.name
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.redirect
WHERE t2.name = 'd'