
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
表1.字段1,
表2.字段2
FROM
表1
INNER
JOIN
表2
ON
表1.字段1
=
表2.字段1
这样的语句可以放在存储过程里declare @id int
insert into table1 (name,password) values (...)
set @id=@@identity --取到刚插入的id
insert into table2 (age,sex,userid) values (...@id)
其实这样就可以了。如果你担心两个表的数据不同步,比如可能插入了table1后,但是出错了,表1有数据但表2没有,你可以把这2条语句放一个事务里。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)