如何解决 MySQL 中不明确的列名错误?

发布于 2024-11-20 00:26:09 字数 529 浏览 5 评论 0原文

可能的重复:
1052:字段列表中的列“id”不明确 < /p>

我有两个表,我想通过 sb_id 连接表(两个表的 sb_id 相同)。所以,我这样使用我的查询:

SELECT Name, 
       class 
  FROM student_info,
       student_class 
 WHERE id = 1 
   AND student_info.sb_id = student_class.sb_id;

它显示错误:

1052:where 子句中的“id”不明确

另一件事,我想通过使用“JOIN”仅显示一个结果。

Possible Duplicate:
1052: Column 'id' in field list is ambiguous

I have two tables, and I want to connect the tables by sb_id (sb_id is same for two tables). So, I used my query like this:

SELECT Name, 
       class 
  FROM student_info,
       student_class 
 WHERE id = 1 
   AND student_info.sb_id = student_class.sb_id;

And it's showed error:

1052: 'id' in where clause is ambiguous

Another thing,I want to show just a single result,by using "JOIN".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

二智少女 2024-11-27 00:26:10

您的 where id=1 应该是 where Student_info.sb_id = 1 吗?

您可以将其更改为如下连接:

SELECT Name,class
来自学生信息
INNER JOIN Student_class ON Student_info.sb_id = Student_class.sb_id
哪里student_info.sb_id=1

Should your where id=1 be where student_info.sb_id = 1 ?

You could change it to a join like this:

SELECT Name,class
FROM student_info
INNER JOIN student_class ON student_info.sb_id = student_class.sb_id
WHERE student_info.sb_id=1

染火枫林 2024-11-27 00:26:10
SELECT Name,class from student_info join student_class on student_info.sb_id=student_class.sb_id where student_info.id=1;
SELECT Name,class from student_info join student_class on student_info.sb_id=student_class.sb_id where student_info.id=1;
铁轨上的流浪者 2024-11-27 00:26:10
SELECT Name,class 
FROM student_info,student_class 
WHERE student_info.sb_id=1 AND student_info.sb_id=student_class.sb_id;
SELECT Name,class 
FROM student_info,student_class 
WHERE student_info.sb_id=1 AND student_info.sb_id=student_class.sb_id;
谎言月老 2024-11-27 00:26:10
SELECT student_info.Name,student_info.class from student_info inner join student_class on student_info.sb_id=student_class.sb_id where student_info.id=1;
SELECT student_info.Name,student_info.class from student_info inner join student_class on student_info.sb_id=student_class.sb_id where student_info.id=1;
草莓味的萝莉 2024-11-27 00:26:10

这意味着两个表中都存在 id 列,只需将 id 替换为 Student_info.id 或 Student_class.id 即可

this means id column is present in both tables, just replace the id with student_info.id or student_class.id which ever is intended

夏九 2024-11-27 00:26:10

在 where 子句中的 ID 列前面加上表名作为前缀。它很困惑,因为 ID 位于两个表中,并且它不知道您指的是哪一个。

WHERE student_info.id=1

或者

WHERE student_class.id=1

取决于您在编写查询时的意思。

Prefix the ID column in the where clause with the table name. It is confused because ID is in both tables and it doesn't know which one you mean.

WHERE student_info.id=1

or

WHERE student_class.id=1

depending on which one you meant when you wrote the query.

笑看君怀她人 2024-11-27 00:26:10

这是因为您的两个表都有 id 字段,因此您需要指定要使用哪个 id (您可以使用表别名,也可以不使用表别名,我更喜欢使用,但这真的取决于你)。此外,使用 JOIN 来连接表比将所有内容都放在 FROM 中要好得多。我会将您的查询重写为

SELECT Name,class 
from student_info si
INNER JOIN student_class sc ON (sc.sb_id = si.sb_id)
WHERE si.id = 1  // or sc.id =1, whatever makes sense

It's because both of your tables have id field, so you need to specify which id you want to use (you may or may not use table aliases, I prefer to use, but it's really up to you). Also, using JOIN for joining tables is much better practice than putting everything in FROM. I'd rewrite your query to

SELECT Name,class 
from student_info si
INNER JOIN student_class sc ON (sc.sb_id = si.sb_id)
WHERE si.id = 1  // or sc.id =1, whatever makes sense
魂牵梦绕锁你心扉 2024-11-27 00:26:09

这意味着两个表都有id,并且您需要在它前面加上它出现的表的前缀(如果两个表中的id相同,则任何一个都可以)。

SELECT
  name,
  class
FROM student_info, student_class
WHERE
  student_info.id=1
  AND student_info.sb_id=student_class.sb_id;

只要恰好有一条带有 student_info.id=1 的记录和一条带有匹配 sb_id 的 student_class 记录,这就会根据您的需要返回单个结果。结果与使用 INNER JOIN 的结果相同 - 换句话说,两条记录必须存在并且连接在一起。

相应的 INNER JOIN 语法如下所示:

SELECT
  name,
  class,
FROM student_info
INNER JOIN student_class ON student_info.sb_id = student_class.sb_id
WHERE student_info.id = 1

That means that both tables have id and you need to prefix it with the table that it appears in (if it is the same in both, either will do).

SELECT
  name,
  class
FROM student_info, student_class
WHERE
  student_info.id=1
  AND student_info.sb_id=student_class.sb_id;

This will return a single result, as you desire, as long as there is exactly one record with student_info.id=1 and a student_class record with a matching sb_id. The result is the same as if you used INNER JOIN — in other words, both records must exist and are joined together.

The corresponding INNER JOIN syntax would look like this:

SELECT
  name,
  class,
FROM student_info
INNER JOIN student_class ON student_info.sb_id = student_class.sb_id
WHERE student_info.id = 1
套路撩心 2024-11-27 00:26:09

问题在于您的 WHERE 语句,您需要使用:

where student_info.id=1

或:

where student_class.id=1

The problem is with your WHERE statment, you need to use:

where student_info.id=1

or:

where student_class.id=1
万水千山粽是情ミ 2024-11-27 00:26:09

从student_info中选择a.Name,a.class作为a,加入student_class作为b
在 a.sb_id = b.sb_id 上
其中id=1;

SELECT a.Name,a.class from student_info as a join student_class as b
on a.sb_id = b.sb_id
where id=1;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文