SQL两张表如何关联

时间:2025-03-05 21:38:44
比如:我有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);

这样是查询了两遍效率不高。不知道直接关联查询的方法是什么?收起