gpt4 book ai didi

mysql 查询错误 1054 - 未知列

转载 作者:行者123 更新时间:2023-11-29 06:47:50 24 4
gpt4 key购买 nike

我在 mysql 中运行查询失败。我故意不想做 select id from roles...

查询

select rtu.role_id
from roles r
where id =(select role_id
from roles_to_user rtu
where user_id=1)

错误

ERROR 1054 (42S22): Unknown column 'rtu.role_id' in 'field list'

desc roles_to_user:

+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| user_id | int(11) | NO | PRI | NULL | |
| role_id | int(11) | NO | PRI | NULL | |
+---------+---------+------+-----+---------+-------+

描述角色:

+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(80) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+

最佳答案

您不能像这样在其外部使用内部选择中的列。选择 r.id 而不是

select r.id
from roles r
where id = (select role_id
from roles_to_user rtu
where user_id = 1)

顺便说一句,如果内部选择返回超过 1 条记录,这将失败。使用 join 代替

select rtu.role_id
from roles r
inner join roles_to_user rtu on rtu.role_id = r.id
where rtu.user_id = 1

关于mysql 查询错误 1054 - 未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17553990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com