比如:我有table1 和 table2 兩張表table1: id name sex 1 張三 男 2 李四 女 3 王五 男table2: ...比如:我有table1 和 table2 兩張表
table1:
id name sex
1 張三 男
2 李四 女
3 王五 男
table2:
id hobby Lid
1 下棋 2
2 游戲 3
3 音樂 2
4 學習 1
我現在想當table1表里查詢出id=2的數據時同時查出table2表里 Lid=2的數據
我以前使用的是兩次查詢方法感覺效率要低很多。
select * from table1 where id=1;
先查出table1表內容 在
select * from table2 where Lid in(select * from table1 where id=1);
這樣是查詢了兩遍效率不高。不知道直接關聯查詢的方法是什么?收起
table1:
id name sex
1 張三 男
2 李四 女
3 王五 男
table2:
id hobby Lid
1 下棋 2
2 游戲 3
3 音樂 2
4 學習 1
我現在想當table1表里查詢出id=2的數據時同時查出table2表里 Lid=2的數據
我以前使用的是兩次查詢方法感覺效率要低很多。
select * from table1 where id=1;
先查出table1表內容 在
select * from table2 where Lid in(select * from table1 where id=1);
這樣是查詢了兩遍效率不高。不知道直接關聯查詢的方法是什么?收起
查詢id寫錯的主要這個意思?
最佳答案
select * from table1 a,table2 b
where a.id = b.lid
追問
嗯,謝謝了的確是這樣的不過應該
select * from table1 a,table2 b where a.id=1 and a.id=b.lid 這樣才算完美了