mysql - 如何根据同一行中的 id 获取名称?
问题描述
所以我是 mySQl 的新手,我正在努力学习它。我想知道约翰斯厨师的名字。所以 John Chefnr 是 100。我想查询一下,我得到 chefname Frank 作为输出。我该怎么做呢?帮助表示赞赏!
+-----+-------+--------+
| eNr | name | chefNr |
+-----+-------+--------+
| 120 | John | 100 |
+-----+-------+--------+
| 100 | Frank | 200 |
+-----+-------+--------+
解决方案
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 |
推荐阅读
- android - GridView 适配器出现问题
- c# - 正则表达式
- php - php 正则表达式检查字符串是否以包含语言代码的特定字符串开头
- ios - ld:架构 arm64 的 81 个重复符号
- sql - 如何使用 Oracle SQL 生成以下序列?
- php - 使用 PHP 变量动态设置多个文本框名称并从中获取值?
- configuration - 赛普拉斯配置是否缓存?
- spring-boot - 处理 JWT SignatureException 的最佳方法
- javascript - 如何使用 d3.js 在螺旋图上绘制度数
- ruby-on-rails - 如何在 Rails rake 任务中运行验证