I have two tables which I join so that I may compare a field and extract records from one table where the field being compared is not in both tables:
Table A
---------
Comp Val
111 327
112 234
113 265
114 865
Table B
-----------
Comp2 Val2
111 7676
112 5678
So what im doing is to join both tables on Comp-Comp2, then I wish to select all values from Table A for which a corrssponding Comp does not exist in Table B. In this case, the query should result in:
Result
---------
Comp Val
113 265
114 865
Here is the query:
select * into Result from TableA
inner join TableB
on (TableB.Comp2 = TableA.Comp)
where TableB.Comp2 <> TableA.Comp
Problem is, it pulls values from both tables. Is there a way to select values from TableA alone without specifying the fields explicitly?
JOIN returns all rows from tables where the key record of one table is equal to the key records of another table. The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables.
Yes, Tables Can Be Joined Without the JOIN Keyword.
For inner joins, the order of the join operations does not affect the query (it can affect the ordering of the rows and columns, but the same data is returned). In this case, the result set is a subset of the Cartesian product of all the tables. The ordering doesn't matter.
Inner Join is the method of retrieval of data from multiple tables based on a required condition and necessary conditions are that there must be common columns or matched columns between the two tables of the database and the data types of columns must be the same.
Just prefix the *
with the desired table name, like this:
select TableA.* into Result from TableA
inner join TableB
on (TableB.Comp2 = TableA.Comp)
where TableB.Comp2 <> TableA.Comp
SELECT a.*
FROM tblA a,tblB b
WHERE a.comp <> b.comp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With