开发者

Join SQL query to get data from two tables

I'm a newbie, just learni开发者_开发问答ng SQL and have this question: I have two tables with the same columns. Some registers are in the two tables but others only are in one of the tables. To illustrate, suppose table A = (1,2,3,4), table B=(3,4,5,6), numbers are registers. I need to select all registers in table B if they are not in table A, that is result=(5,6). What query should I use? Maybe a join. Thanks.


You can either use a NOT IN query like this:

SELECT col from A where col not in (select col from B)

or use an outer join:

select A.col from A LEFT OUTER JOIN B on A.col=B.col where B.col is NULL

The first is easier to understand, but the second is easier to use with more tables in the query.


Select register from TABLE_B b
Where not exists (Select register from TABLE_A a where a.register = b.register)

I assumed you have a column named register in TABLE_A and TABLE_B

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜