ZKX's LAB

sql将列不相等的两表横连接 SQL如何将两个列数不一样的表的内容纵向拼接在一起

2021-04-04知识3

如何保证SQL里的两列不能同时相等? 我的数据库中,有两个表,一级分类表tb1,有如下列:id,name;二级分类表tb2,有如下列:id,name,tb1id.其中tb1id引用的tb1中的id列.我该如何确保tb2中的数据里的name和tb1id不同时相等?use tempdbgocreate table tb2(ID int primary key,Name nvarchar(50)not null,tb1id int)gocreate function Chk_Check(@tb1id int,@Name nvarchar(50))returns bitasbeginif(select count(1)from tb2 where Name=Name and tb1id=tb1id)=1return 1return 0endgoalter table tb2 add constraint Chk_Check_Name check(dbo.Chk_Check(tb1ID,Name)=1)go测试insert tb2 select 1,'a',1insert tb2 select 2,'a',1-出错insert tb2 select 3,'a',2insert tb2 select 4,'a',2-出错select*from tb2

如何将SQL两个表中某一字段不一样的行筛选出来? 假设两个表A和B,如你所说:A比B的数据要多,找出A中多出的数据就用not in,col为他们的公共列select*from A where col not in(select col from B)

怎么连接sql中的两个表的不同的列 可以用union all 但是字段数必须一样多比如select id,name from 表bunion allselect sex,bz from 表a或者可以见一个相关的字段,进行连接

#sql将列不相等的两表横连接

随机阅读

qrcode
访问手机版