首页 > 解决方案 > 如何根据同一行中的 id 获取名称?

问题描述

所以我是 mySQl 的新手,我正在努力学习它。我想知道约翰斯厨师的名字。所以 John Chefnr 是 100。我想查询一下,我得到 chefname Frank 作为输出。我该怎么做呢?帮助表示赞赏!

+-----+-------+--------+
| eNr | name  | chefNr |
+-----+-------+--------+
| 120 | John  | 100    |
+-----+-------+--------+
| 100 | Frank | 200    |
+-----+-------+--------+

标签: mysqlsqldatabase

解决方案


This query:

select chefnr from tablename where name = 'John'

will return 100 in the column chefnr.
You can use it like this:

select name from tablename
where enr = (select chefnr from tablename where name = 'John')

to get the name of John's chef.
See the demo.
Result:

| name  |
| ----- |
| Frank |

推荐阅读