SQL语句中,查询一个结果,满足表1的A条件,满足表2的B条件,怎么写?

2025-06-29 06:13:05
推荐回答(4个)
回答1:

1.创建测试表,

创建表test_col_1(id号,varvarchar2(200));

创建表test_col_2(id号,varvarchar2(200));

2.插入测试数据,

insertintotest_col_1

选择level*8, 'var'||*8 from dual connect by level <= 20;

insertintotest_col_2

选择level,‘var’||level from dual connect by level <= 100;

3.查询表A和表B中的相关记录,

Select*

fromtest_col_2b

Whereexists(select1fromtest_col_1awhereb。id=a.id)

4.查询表A中的所有数据和A、B中的相关数据,

Select*

fromtest_col_1a

unionall

Select*

fromtest_col_2b

Whereexists(select1fromtest_col_1awhereb。id=a.id)

回答2:

select * from 表1,表2 where A条件 AND B条件 AND 表1.a=表2.b
这个,主要你的表1和表2 之间要有关系啊,没关系的话不好写。

回答3:

假如表1,表2分别为table1,table2,关联字段是id,那么
select t1.*, t2.* from table1 t1, table2 t2 where t1.id = t2.id and t1.字段 = A条件 and t2.字段 = B条件

左连接就是left join啊
select * from table1 t1 left join table2 t2 on t1.id = t2.id where t1.字段 = A条件 and t2.字段 = B条件

回答4:

问:表1,表2是通过那个字段关联。