1、创建两张测试表,
create table test_cj(name VARCHAR(20), remark varchar2(20));
create table test_kc(name VARCHAR(20), remark varchar2(20));
2、插入测试数据
insert into test_cj values('xh','cj_1');
insert into test_cj values('kcdh','cj_2');
insert into test_cj values('cj','cj_3');
insert into test_kc values('kcdh','kc_1');
insert into test_kc values('kcm','kc_2');
3、查询两张表的总贺猜衡记录数禅做,select t.*, rowid from test_cj t union all select t.*, rowid from test_kc t,
4、编写sql,两张表进行关联,select t.name, t.remark, b.remark from test_cj t, test_kc b where t.name=b.name,可兆启以发现关联出kcdh的记录,
select *
from cj join kc on cj.kcdh=kc.kcdh
--就这么简单。这样保证两个表的数据都能滚数弊查出来。
--inner join内大族连接将会过滤掉不存在关联的数据的毕洞。
select * from cj表,kc表 where cj表轮激.kcdh=kc表腊猜袜.kcdh
或者兆此 select * from cj表 where kcdh=(select kcdh from kc表)
select cj.xh, cj.kcdh, cj.cj, kc.kcdh, kc.kcm
from cj inner join kc on (cj.kcdh = kc.kcdh)
SQL SERVER
PS 用inner join 最态握念好帆困皮伏。
Select a.xh,a.kcdh,a.cj,b.kcdh,b.kcm from cj as a,kc as b where a.kcdh=b.kcdh