Jerry Merry
Jerry Merry

Reputation: 31

query data from one table that matches with value from another table

I have two table created as

create table table1(id int,);
create table table2(id int, tb2_id int,...)

But when i try out

Select * from table2 where tb2_id=table1.id;

I have got an error that table1.id is an unknown column.

Could someone point out where the mistake I made is ?

Upvotes: 1

Views: 1292

Answers (4)

John Woo
John Woo

Reputation: 263933

try this:

SELECT * 
FROM Table2
WHERE ID IN (SELECT ID FROM Table1)

Upvotes: 1

Juicy Scripter
Juicy Scripter

Reputation: 25938

You probably want to JOIN tables:

SELECT table2.* FROM table2 JOIN table1 ON (table2.tb2_id=table1.id)

Upvotes: 3

jmoreno
jmoreno

Reputation: 13571

You need either a join or a subquery.

Select t2.*
from table2 t2
  Inner join table1 t1
     On t2.tbl2_id = t1.id

Or

Select t2.*
from table2 t2
where tbl2_id in ( select id from table1 )

Upvotes: 1

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

Select * from table2, table1 where tb2_id=table1.id;

Upvotes: 1

Related Questions