1

I am having 2 tables named table1, table2. table1 columns are t1_id, t1_name, t1_status. table2 columns are t1_id, t2_id, t2_name, t2_status. When I do Some operation in frontend t1_id will be inserted into table2. What I need is I want t1_id(s) from table1 which are not alloted or inserted in table2.

I've tried this query:

SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2 
ON t1.t1_id != t2.t1_id

The problem with query is when all t1_id(s) are inserted into table2, then all t1_id(s) are showing again. How to resolve this issue? (I'am new to sql so please don't consider my mistakes.)

Cœur
  • 37,241
  • 25
  • 195
  • 267
chikku
  • 863
  • 1
  • 7
  • 19
  • see "Left Excluding JOIN" at this reference http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins , & as you are new to SQL keep this reference handy – Paul Maxwell Aug 12 '15 at 07:55

3 Answers3

1

Use is null to get rows from table1 which don't have any associations in table2

SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2 
ON t1.t1_id = t2.t1_id
WHERE t2.t1_id IS NULL
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
1

If i understand your question right, this must b the query you need:

SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2 
ON t1.t1_id = t2.t1_id
where t2.t1_id is null
Jens
  • 67,715
  • 15
  • 98
  • 113
0

I think this answers your questions: Select rows which are not present in other table

SELECT t1.t1_id, t1.t1_name
FROM table1 t1 
WHERE NOT EXISTS (
SELECT 1    --it's not so relevant what you put here
FROM table2 t2 
WHERE t1.t1_id = t2.t1_id
)

I hope this helps. ;-)

Community
  • 1
  • 1
Tech0
  • 253
  • 3
  • 16