SQL语言中把数据库中两张表数据关联起来的语句

2024-11-18 12:34:31
推荐回答(5个)
回答(1):

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的记录,

回答(2):

select *
from cj join kc on cj.kcdh=kc.kcdh

--就这么简单。这样保证两个表的数据都能滚数弊查出来。
--inner join内大族连接将会过滤掉不存在关联的数据的毕洞。

回答(3):

select * from cj表,kc表 where cj表轮激.kcdh=kc表腊猜袜.kcdh

或者兆此 select * from cj表 where kcdh=(select kcdh from kc表)

回答(4):

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 最态握念好帆困皮伏。

回答(5):

Select a.xh,a.kcdh,a.cj,b.kcdh,b.kcm from cj as a,kc as b where a.kcdh=b.kcdh