Skip to main content
descriptive title, add tag
Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

Help with this SELECT Select rows where value of second column is not present in the same tablefirst column

tag, typos, format
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

I have this table structure:

 id1 id2
 1   2 
 1   3
 1   4
 2   1
 2   5

I need to build a SQLquery to select id2id2 where id1id1 is not in id2id2. For example if id1 = 1id1 = 1 then only id2=3id2=3 and id2=4id2=4. Any help? I try with
I tried this one:

SET @a=1;
SELECT DISTINCT x.id2 
 FROM tt x, tt y 
 WHERE x.id1=@a AND x.id1 != y.id2;

but itsn'tit's not working.

I have this table structure:

 id1 id2
 1   2 
 1   3
 1   4
 2   1
 2   5

I need to build a SQL to select id2 where id1 is not in id2. For example if id1 = 1 then only id2=3 and id2=4. Any help? I try with this one:

SET @a=1;
SELECT DISTINCT x.id2 
 FROM tt x, tt y 
 WHERE x.id1=@a AND x.id1 != y.id2;

but itsn't working

I have this table structure:

 id1 id2
 1   2 
 1   3
 1   4
 2   1
 2   5

I need to build a query to select id2 where id1 is not in id2. For example if id1 = 1 then only id2=3 and id2=4.
I tried this one:

SET @a=1;
SELECT DISTINCT x.id2 
 FROM tt x, tt y 
 WHERE x.id1=@a AND x.id1 != y.id2;

but it's not working.

Tweeted twitter.com/#!/StackDBAs/status/192388915372630017
Source Link
ReynierPM
  • 1.9k
  • 10
  • 31
  • 49

Help with this SELECT in the same table

I have this table structure:

 id1 id2
 1   2 
 1   3
 1   4
 2   1
 2   5

I need to build a SQL to select id2 where id1 is not in id2. For example if id1 = 1 then only id2=3 and id2=4. Any help? I try with this one:

SET @a=1;
SELECT DISTINCT x.id2 
 FROM tt x, tt y 
 WHERE x.id1=@a AND x.id1 != y.id2;

but itsn't working