|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
WHERE EXISTS clause in BOL--SCENARIO #1 (from BOL) USE pubs GO SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE titles.pub_id = publishers.pub_id AND titles.type = 'business') GO --SCEANRIO #2 USE pubs GO SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles, publishers WHERE titles.pub_id = publishers.pub_id AND titles.type = 'business') GO In the first query, the sub-select is comparing the value of titles.pub_id to
the value for publishers.pub_id retreived by the outer select. In the second query, the sub-select is comparing the value of titles.pub_id to the value of publishers.pub_id within the same SELECT statement, instead of referencing the value from the outer select. Try using a table alias: SELECT DISTINCT pub_name FROM publishers t1 WHERE EXISTS (SELECT * FROM titles, publishers WHERE titles.pub_id = t1.pub_id --note difference AND titles.type = 'business') GO -- Show quote"Bullfrog1870" wrote: > Why do I get different results when I include 'publishers' to the inner query? > > --SCENARIO #1 (from BOL) > USE pubs > GO > SELECT DISTINCT pub_name > FROM publishers > WHERE EXISTS > (SELECT * > FROM titles > WHERE titles.pub_id = publishers.pub_id > AND titles.type = 'business') > GO > > --SCEANRIO #2 > USE pubs > GO > SELECT DISTINCT pub_name > FROM publishers > WHERE EXISTS > (SELECT * > FROM titles, publishers > WHERE titles.pub_id = publishers.pub_id > AND titles.type = 'business') > GO > |
|||||||||||||||||||||||