日期:2014-05-16 浏览次数:20699 次
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-04 08:34:45
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[TEST1]
if object_id('[TEST1]') is not null drop table [TEST1]
go
create table [TEST1]([ID] int,[BH] int,[XC] int,[SL] int)
insert [TEST1]
select 1,1622,1,100 union all
select 2,1622,2,300 union all
select 3,1700,1,100 union all
select 4,1622,3,150 union all
select 5,1700,2,200 union all
select 6,1800,1,100 union all
select 7,1700,5,100
--> 测试数据[TEST2]
if object_id('[TEST2]') is not null drop table [TEST2]
go
create table [TEST2]([ID] int,[BH] int,[XC] int)
insert [TEST2]
select 1,1622,1 union all
select 2,1800,1 union all
select 3,1700,2
--------------生成数据--------------------------
select a.*
from [TEST1] a INNER JOIN [TEST2] b ON a.BH=b.BH AND a.XC=b.XC
ORDER BY a.xc
----------------结果----------------------------
/*
ID BH XC SL
----------- ----------- ----------- -----------
1 1622 1 100
6 1800