SQL中 inner join、 left join 、right join、 outer join之间的区别[zz]

A表(a1,b1,c1)      B表(a2,b2)
a1 b1 c1 a2 b2
01 数学 95 01 张三
02 语文 90 02 李四
03 英语 80 04 王五
select A.*,B.* from A
inner join B on(A.a1=B.a2)
结果是:
a1 b1 c1 a2 b2
01 数学 95 01 张三
02 语文 90 02 李四

select A.*,B.* from A
left outer join B on(A.a1=B.a2)
结果是:
a1 b1 c1 a2 b2
01 数学 95 01 张三
02 语文 90 02 李四
03 英语 80 NULL NULL

select A.*,B.* from A
right outer join B on(A.a1=B.a2)
结果是:
a1 b1 c1 a2 b2
01 数学 95 01 张三
02 语文 90 02 李四
NULL NULL NULL 04 王五

select A.*,B.* from A
full outer join B on(A.a1=B.a2)
结果是:
a1 b1 c1 a2 b2
01 数学 95 01 张三
02 语文 90 02 李四
03 英语 80 NULL NULL
NULL NULL NULL 04 王五
USE [irepodb] GO /****** Object: StoredProcedure [dbo].[P_34#_BOLT] Script Date: 2025/10/30 16:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- Batch submitted through debugger: SQLQuery24.sql|7|0|C:\Users\jinfeng_xiang\AppData\Local\Temp\~vs3A56.sql -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[P_34#_BOLT] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. declare @val VARCHAR(10),@displayValue VARCHAR(10),@Date VARCHAR(20),@celladdress VARCHAR(15),@rep_top_id VARCHAR(10),@def_top_id VARCHAR(10), @error int ,@differ VARCHAR(10),@workpiece VARCHAR(10),@specs VARCHAR(10),@orders VARCHAR(10),@time VARCHAR(10),@remarks nvarchar(50) ,@sheetNo VARCHAR(10) ,@def_top_org VARCHAR(10) set @error=0 INSERT INTO T_BOLT_TREND_B SELECT * FROM T_BOLT_TREND WHERE def_top_id in ('411','10322','21529') DELETE FROM T_BOLT_TREND WHERE def_top_id in ('411','10322','21529') -- begin tran --申明事务 declare row_cursor_col cursor FOR (SELECT distinct displayValue ,celladdress,rep_top_id,def_top_id,sheetNo,def_top_org from [irepodb].[dbo].[T_BOLT] WHERE (type='InputNumeric' OR type='KeyboardText') AND CONVERT(int ,REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','')) >= '6' and def_top_org in ('411','10322','21529') and displayValue is not null ) open row_cursor_col --开始循环游标变量-- fetch next from row_cursor_col into @displayValue,@celladdress,@rep_top_Id,@def_top_id,@sheetNo,@def_top_org while @@FETCH_STATUS = 0 --返回被 FETCH语句执行的最后游标的状态-- begin SELECT @Date=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE type='Date' AND REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') = '2' and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo SELECT @workpiece=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE REPLACE(SUBSTRING(CELLADDRESS,2,2),'$','')=REPLACE(SUBSTRING(@celladdress,2,2),'$','') and type='Select' -- and name='班组' AND REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') = '3' and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo --G列=71 IF (dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))-71)%3=1 BEGIN SELECT @time=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE type='Time' -- and name='时间' AND REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') = '4' AND dbo.getTransCOL(REPLACE(SUBSTRING(celladdress,2,2),'$',''))=dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))+1 and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo SET @differ='最大值' SELECT @specs=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE --type='KeyboardText' AND (REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') BETWEEN CONVERT(INT,REPLACE(SUBSTRING(@celladdress,4,2),':','')) AND CONVERT(INT,REPLACE( RIGHT(rtrim(@celladdress), 2),'$',''))) AND dbo.getTransCOL(REPLACE(SUBSTRING(celladdress,2,2),'$',''))=dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))+2 and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo SET @orders=(dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))-69)/3 insert into T_BOLT_TREND (ID,Date,TIME,Workpiece,Differ,DisplayValue,Orders,COL,Specs,updateTime,rep_top_id,def_top_id,sheetNo,Remarks) values(NEWID(), @Date,@time,@workpiece,@differ,@displayValue,@orders,REPLACE(@celladdress,' ',''),@specs,GETDATE(),@rep_top_id,@def_top_org,@sheetNo,'ZZ') END IF (dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))-71)%3=2 BEGIN SELECT @time=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE type='Time' -- and name='时间' AND REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') = '4' AND dbo.getTransCOL(REPLACE(SUBSTRING(celladdress,2,2),'$',''))=dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$','')) and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo SET @differ='最小值' SELECT @specs=ISNULL(displayValue,0) from [irepodb].[dbo].[T_BOLT] WHERE (REPLACE( RIGHT(rtrim(CELLADDRESS), 2),'$','') BETWEEN CONVERT(INT,REPLACE(SUBSTRING(@celladdress,4,2),':','')) AND CONVERT(INT,REPLACE( RIGHT(rtrim(@celladdress), 2),'$',''))) AND dbo.getTransCOL(REPLACE(SUBSTRING(celladdress,2,2),'$',''))=dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))+1 and @rep_top_id = rep_top_id and @def_top_org=def_top_org and @sheetNo=sheetNo SET @orders=(dbo.getTransCOL(REPLACE(SUBSTRING(@celladdress,2,2),'$',''))-70)/3 insert into T_BOLT_TREND (ID,Date,TIME,Workpiece,Differ,DisplayValue,Orders,COL,Specs,updateTime,rep_top_id,def_top_id,sheetNo,Remarks) values(NEWID(), @Date,@time,@workpiece,@differ,@displayValue,@orders,REPLACE(@celladdress,' ',''),@specs,GETDATE(),@rep_top_id,@def_top_org,@sheetNo,'ZZ') END -- print @time -- print @Date -- print @workpiece -- print @displayValue -- print @specs -- print @celladdress -- print @rep_top_id set @error= @error + @@ERROR --记录每次运行sql后是否正确,0正确 fetch next from row_cursor_col into @displayValue,@celladdress,@rep_top_Id,@def_top_id,@sheetNo,@def_top_org --转到下一个游标 end INSERT INTO T_BOLT_KANBAN SELECT A.Specs, A.DATE, A.TIME, A.Workpiece, A.Differ, A.COL, CONVERT(DECIMAL(12, 2), A.DisplayValue) AS DisplayValue, A.Orders, A.rep_top_id, A.def_top_id, A.sheetNo, B.PROJECTID, B.PROJECT, B.ROW, B.BOLT_NUM, B.LINE, B.TEAM, B.TYPE, B.UP_LIMIT, B.DOWN_LIMIT, B.UP_ACTION, B.DOWN_ACTION,GETDATE() FROM dbo.T_BOLT_TREND AS A LEFT OUTER JOIN dbo.T_BOLT_LIMIT AS B ON A.def_top_id = B.DEF_TOP_ORG AND B.ROW = REPLACE(RIGHT(RTRIM(A.COL), 2), '$', '') WHERE DEF_TOP_ORG in ('411','10322','21529') AND ((dbo.getTransCOL(REPLACE(SUBSTRING(A.COL, 2, 2), '$', '')) - 71) % 3 <> 0) -- if @error=0 --begin -- commit tran --提交事务 --end -- else --begin -- rollback tran --回滚事务 --end close row_cursor_col --关闭游标 deallocate row_cursor_col --释放游标 END 优化存储过程
最新发布
10-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值