exists 的简单介绍
准备数据:
1 CREATE TABLE Books( 2 BookID number, 3 BookTitle VARCHAR2(20) NOT NULL, 4 Copyright varchar2(20) 5 ) 6 INSERT INTO Books VALUES (12786, 'Java',1934); 7 INSERT INTO Books VALUES (13331, 'MySQL',1919); 8 INSERT INTO Books VALUES (14356, 'PHP',1966); 9 INSERT INTO Books VALUES (15729, 'PERL',1932); 10 INSERT INTO Books VALUES (16284, 'Oracle',1996); 11 INSERT INTO Books VALUES (17695, 'Pl/SQL',1980); 12 INSERT INTO Books VALUES (19264, 'JavaScript',1992); 13 INSERT INTO Books VALUES (19354, 'www.manongjc.com',1993); 14 15 CREATE TABLE AuthorBook( 16 AuthID NUMBER, 17 BookID NUMBER 18 ) 19 20 INSERT INTO AuthorBook VALUES (1006, 14356); 21 INSERT INTO AuthorBook VALUES (1008, 15729); 22 INSERT INTO AuthorBook VALUES (1009, 12786); 23 INSERT INTO AuthorBook VALUES (1010, 17695); 24 INSERT INTO AuthorBook VALUES (1011, 15729); 25 INSERT INTO AuthorBook VALUES (1012, 19264); 26 INSERT INTO AuthorBook VALUES (1012, 19354); 27 INSERT INTO AuthorBook VALUES (1014, 16284);
BOOKS表:
AUTHORBOOK表:
查询B表ID在A表中的数据
1 select * from BOOKS B 2 WHERE EXISTS (SELECT * FROM AUTHORBOOK A WHERE A.BOOKID=B.BOOKID)
EXISTS后面表示的只有两个值。就是“真”或者“假”。 NOT EXISTS 与EXISTS相反。