简体   繁体   中英

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

I have the following data:

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

I run the following MySQL code

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

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

SQLException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio

In MySQL, you need a table alias after any subquery. 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 . This is a name for the subquery and is required by MySQL. The name "t" is arbitrary. You can give it any valid name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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