[英]query data from one table that matches with value from another table
我有兩個表創建為
create table table1(id int,);
create table table2(id int, tb2_id int,...)
但是當我嘗試
Select * from table2 where tb2_id=table1.id;
我有一個錯誤,說table1.id是未知列。
有人可以指出我犯的錯誤在哪里嗎?
您可能想JOIN
表:
SELECT table2.* FROM table2 JOIN table1 ON (table2.tb2_id=table1.id)
Select * from table2, table1 where tb2_id=table1.id;
您需要連接或子查詢。
Select t2.*
from table2 t2
Inner join table1 t1
On t2.tbl2_id = t1.id
要么
Select t2.*
from table2 t2
where tbl2_id in ( select id from table1 )
嘗試這個:
SELECT *
FROM Table2
WHERE ID IN (SELECT ID FROM Table1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.