Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner Join Tables But Select From One Table Only

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?

like image 757
Kiera Smith Avatar asked Jul 09 '15 18:07

Kiera Smith


People also ask

Can we use inner join for single table?

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.

Can we select from multiple tables without join?

Yes, Tables Can Be Joined Without the JOIN Keyword.

Does inner join Change the table?

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.

How does inner join work for multiple tables?

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.


2 Answers

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
like image 103
sstan Avatar answered Oct 31 '22 02:10

sstan


SELECT a.* 
FROM tblA a,tblB b 
WHERE a.comp <> b.comp
like image 23
Nemrod Mondez Avatar answered Oct 31 '22 02:10

Nemrod Mondez