what does this query mean
what does this query mean
select fname, lname
from Owner
where not exists
(select fname, lname
from Trainer)
what i understand :it returns fname and lname from table owner where these names not exsit in table traine开发者_如何转开发r ? am i right?
Find the names of people who exist in the Owner
table but not in the Trainer
table.
It's a pretty lame way to get a list of everyone from the owner
table that doesn't exist in the trainer
table.
So, if the Owner
table contains
----------------
fname | lname
----------------
clark | kent
lois | lane
peter | parker
and the Trainer table contains
----------------
fname | lname
----------------
peter | parker
hal | jordan
You should get a result set:
----------------
fname | lname
----------------
clark | kent
lois | lane
Update
Actually, your query won't return anything if there are any records at all in the Trainer table. You should probably be using:
select fname, lname
from Owners
where not exists (
select fname, lname
from trainers
where fname=Owners.fname
and lname=Owners.lname
)
The query has two parts: the 'select' and the 'where'. Look at the where part first:
where not exists
(select fname, lname from Trainer)
This where clause evaluates to 'true' if there is nothing in the Trainer table, and to 'false' if there is something there. Now look at the 'select' part:
select fname, lname from Owners
This selects all rows from the 'Owners' table.
Putting this together: the query selects all rows from the Owner table IF (and only if) there is nothing in the Trainer table -- and absolutely nothing if there is.
精彩评论