简体   繁体   English

为什么我不能从MySQL的子查询中选择一列?

[英]Why can't I select one column from a subquery in MySQL?

I have the following data: 我有以下数据:

http://i.imgur.com/e4d8M8V.png?1 http://i.imgur.com/e4d8M8V.png?1

I run the following MySQL code 我运行以下MySQL代码

select distinct i.InstructorID, i.Salary
from Instructor i
where (i.InstructorID NOT IN (select o.InstructorID from Offering o));

Which gives the desired output here: 在此处给出所需的输出:

http://i.imgur.com/rkFKseX.png?1 http://i.imgur.com/rkFKseX.png?1

How could I get just the salaries from this query? 我如何从该查询中获取薪水?

I've tried 我试过了

$MySQL:>select (i.Salary)
from (select distinct i.InstructorID, i.Salary
from Instructor i
where (i.InstructorID NOT IN (select o.InstructorID from Offering o)));

as well as trying to change the first select parameter to i1.Salary or just simply (Salary), but no matter what I do I just get a syntax error 以及尝试将第一个选择参数更改为i1.Salary或只是将其更改为(Salary),但是无论我做什么我都只会遇到语法错误

SQLException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio

In MySQL, you need a table alias after any subquery. 在MySQL中,在任何子查询之后都需要一个表别名。 So the following should work: 因此,以下应该工作:

select t.Salary
from (select distinct i.InstructorID, i.Salary
      from Instructor i
      where (i.InstructorID NOT IN (select o.InstructorID from Offering o))
     ) t;

Note the t . 注意t This is a name for the subquery and is required by MySQL. 这是子查询的名称,MySQL必需。 The name "t" is arbitrary. 名称“ t”是任意的。 You can give it any valid name. 您可以给它提供任何有效的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM