一条SQL语句

xiaowangtian1117 2009-09-15 03:11:12
两个表:
TEST1:
A B C
F0 2009-9-1 2
F0 2009-9-2 2
F0 2009-9-4 2
F6 2009-9-1 3
TEST2:
D E F
F0 2009-9-1 4
F0 2009-9-3 4
F0 2009-9-4 4
F6 2009-9-2 5
需要算出A=D和B=E的C*F
出来结果:
F0 2009-9-1 8
F0 2009-9-2 2
F0 2009-9-3 4
F0 2009-9-4 8
F6 2009-9-1 3
F6 2009-9-2 5
...全文
121 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
小宏 2009-09-15
  • 打赏
  • 举报
回复
顶海爷。。。。
昵称被占用了 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xiaowangtian1117 的回复:]
如果在加一张表:
TEST3:
G      H      I
F0 2009-9-1  4
出来结果:
F0 2009-9-1  32
F0 2009-9-2  2
F0 2009-9-3  4
F0 2009-9-4  8
F6 2009-9-1  3
F6 2009-9-2  5

[/Quote]
三表full join必须子查询,否则有问题

select 
isnull(ad,g) as adg
,isnull(be,h) as beh
,isnull(cf,1)*isnull(i,1) as cfi
from (
select
isnull(a,d) as ad
,isnull(b,e) as be
,isnull(c,1)*isnull(f,1) as cf
from test1 a full join test2 b
on a.a=b.d and a.b = b.e
) as t
full join test3 t1 on t.ad = t1.g and t.be = t1.h

fanzhouqi 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xiaowangtian1117 的回复:]
如果在加一张表:
TEST3:
G      H      I
F0 2009-9-1  4
出来结果:
F0 2009-9-1  32
F0 2009-9-2  2
F0 2009-9-3  4
F0 2009-9-4  8
F6 2009-9-1  3
F6 2009-9-2  5

[/Quote]
再用 full join ,依样画葫芦
soft_wsx 2009-09-15
  • 打赏
  • 举报
回复
没看明白你们说什么!顶了!
xiaowangtian1117 2009-09-15
  • 打赏
  • 举报
回复
如果在加一张表:
TEST3:
G H I
F0 2009-9-1 4
出来结果:
F0 2009-9-1 32
F0 2009-9-2 2
F0 2009-9-3 4
F0 2009-9-4 8
F6 2009-9-1 3
F6 2009-9-2 5
code_study 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 haiwer 的回复:]
要用full join的
[/Quote]
up
--小F-- 2009-09-15
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-15 15:14:26
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[TEST1]
if object_id('[TEST1]') is not null drop table [TEST1]
go
create table [TEST1]([A] varchar(2),[B] datetime,[C] int)
insert [TEST1]
select 'F0','2009-9-1',2 union all
select 'F0','2009-9-2',2 union all
select 'F0','2009-9-4',2 union all
select 'F6','2009-9-1',3
--> 测试数据:[TEST2]
if object_id('[TEST2]') is not null drop table [TEST2]
go
create table [TEST2]([D] varchar(2),[E] datetime,[F] int)
insert [TEST2]
select 'F0','2009-9-1',4 union all
select 'F0','2009-9-3',4 union all
select 'F0','2009-9-4',4 union all
select 'F6','2009-9-2',5
--------------开始查询--------------------------
select
isnull(a,d) ,isnull(convert(varchar(10),b,120),convert(varchar(10),e,120)) ,isnull(c,1)*isnull(f,1)
from
test1 a
full join test2 b on
a.a=b.d and a.b = b.e
order by
isnull(a,d)
----------------结果----------------------------
/* (4 行受影响)

---- ---------- -----------
F0 2009-09-01 8
F0 2009-09-02 2
F0 2009-09-04 8
F0 2009-09-03 4
F6 2009-09-02 5
F6 2009-09-01 3

(6 行受影响)

*/
--小F-- 2009-09-15
  • 打赏
  • 举报
回复
---改整齐点
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-15 15:14:26
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[TEST1]
if object_id('[TEST1]') is not null drop table [TEST1]
go
create table [TEST1]([A] varchar(2),[B] datetime,[C] int)
insert [TEST1]
select 'F0','2009-9-1',2 union all
select 'F0','2009-9-2',2 union all
select 'F0','2009-9-4',2 union all
select 'F6','2009-9-1',3
--> 测试数据:[TEST2]
if object_id('[TEST2]') is not null drop table [TEST2]
go
create table [TEST2]([D] varchar(2),[E] datetime,[F] int)
insert [TEST2]
select 'F0','2009-9-1',4 union all
select 'F0','2009-9-3',4 union all
select 'F0','2009-9-4',4 union all
select 'F6','2009-9-2',5
--------------开始查询--------------------------
select
isnull(a,d) ,isnull(convert(varchar(10),b,120),convert(varchar(10),e,120)) ,isnull(c,1)*isnull(f,1)
from
test1 a
full join test2 b on
a.a=b.d and a.b = b.e
----------------结果----------------------------
/* ---- ---------- -----------
F0 2009-09-01 8
F0 2009-09-02 2
F0 2009-09-04 8
F6 2009-09-01 3
F0 2009-09-03 4
F6 2009-09-02 5

(6 行受影响)


*/
lihan6415151528 2009-09-15
  • 打赏
  • 举报
回复
select test1.a,test1.b,test1.c*test2.f from
test1 inner join test2
on test1.a=test2.d and test1.b=test2.e
xiequan2 2009-09-15
  • 打赏
  • 举报
回复
--> 测试数据:@tb
declare @tb table([A] varchar(2),[B] datetime,[C] int)
insert @tb
select 'F0','2009-9-1',2 union all
select 'F0','2009-9-2',2 union all
select 'F0','2009-9-4',2 union all
select 'F6','2009-9-1',3
--> 测试数据:@tb2
declare @tb2 table([D] varchar(2),[E] datetime,[F] int)
insert @tb2
select 'F0','2009-9-1',4 union all
select 'F0','2009-9-3',4 union all
select 'F0','2009-9-4',4 union all
select 'F6','2009-9-2',5



select isnull(A,D),isnull( b,e), isnull(c,1)*isnull(f,1)
from @tb tb full join @tb2 tb2 on tb.a=tb2.d and b=e order by isnull(a,d),isnull( b,e)
/*
---- ----------------------- -----------
F0 2009-09-01 00:00:00.000 8
F0 2009-09-02 00:00:00.000 2
F0 2009-09-03 00:00:00.000 4
F0 2009-09-04 00:00:00.000 8
F6 2009-09-01 00:00:00.000 3
F6 2009-09-02 00:00:00.000 5

(6 行受影响)


*/
水族杰纶 2009-09-15
  • 打赏
  • 举报
回复
-->Title:生成測試數據
-->Author:wufeng4552
-->Date :2009-09-15 15:16:04

if not object_id('test1') is null
drop table test1
Go
Create table test1([A] nvarchar(2),[B] Datetime,[C] int)
Insert test1
select N'F0','2009-9-1',2 union all
select N'F0','2009-9-2',2 union all
select N'F0','2009-9-4',2 union all
select N'F6','2009-9-1',3
Go

if not object_id('test2') is null
drop table test2
Go
if not object_id('test2') is null
drop table test2
Go
Create table test2([D] nvarchar(2),[E] Datetime,[F] int)
Insert test2
select N'F0','2009-9-1',4 union all
select N'F0','2009-9-3',4 union all
select N'F0','2009-9-4',4 union all
select N'F6','2009-9-2',5
Go

Go
select isnull(test1.a,test2.d),
isnull(test1.b,test2.e),
isnull(test1.c,1)*isnull(test2.f,1) from
test1 full join test2
on test1.a=test2.d and test1.b=test2.e
/*
---- ----------------------- -----------
F0 2009-09-01 00:00:00.000 8
F0 2009-09-02 00:00:00.000 2
F0 2009-09-04 00:00:00.000 8
F6 2009-09-01 00:00:00.000 3
F0 2009-09-03 00:00:00.000 4
F6 2009-09-02 00:00:00.000 5

*/
fanzhouqi 2009-09-15
  • 打赏
  • 举报
回复
declare @TEST1 table(A  varchar(10),B datetime  ,C int ) 
insert into @Test1
select 'F0', '2009-9-1', 2
union all select 'F0', '2009-9-2', 2
union all select 'F0', '2009-9-4', 2
union all select 'F6', '2009-9-1', 3
declare @TEST2 table(D varchar(10),E datetime ,F int )
insert into @TEST2
select 'F0', '2009-9-1', 4
union all select 'F0', '2009-9-3', 4
union all select 'F0', '2009-9-4', 4
union all select 'F6', '2009-9-2', 5


select A = isnull(a.a,b.d)
,B = isnull(a.b,b.e)
,C = isnull(a.c,1)*isnull(b.f,1)
from @test1 a full join @test2 b
on a.a = b.d and a.b = b.e
order by isnull(a.a,b.d),isnull(a.b,b.e)


A B C
---------- ------------------------------------------------------ -----------
F0 2009-09-01 00:00:00.000 8
F0 2009-09-02 00:00:00.000 2
F0 2009-09-03 00:00:00.000 4
F0 2009-09-04 00:00:00.000 8
F6 2009-09-01 00:00:00.000 3
F6 2009-09-02 00:00:00.000 5

(所影响的行数为 6 行)
zhousq00 2009-09-15
  • 打赏
  • 举报
回复
先做一个连接
然后对单表做乘法

select A,B,C*F FROM
(
select * from TEST1 inner join TEST2
)TEMP
where A=D and B=D


不可以单纯select a.a,a.b,a.c*b.f from test1 a,test2 b where a.a=b.d and a.b=b.e
这样是得不到结果的!
--小F-- 2009-09-15
  • 打赏
  • 举报
回复
---同意海爷意见
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-15 15:14:26
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[TEST1]
if object_id('[TEST1]') is not null drop table [TEST1]
go
create table [TEST1]([A] varchar(2),[B] datetime,[C] int)
insert [TEST1]
select 'F0','2009-9-1',2 union all
select 'F0','2009-9-2',2 union all
select 'F0','2009-9-4',2 union all
select 'F6','2009-9-1',3
--> 测试数据:[TEST2]
if object_id('[TEST2]') is not null drop table [TEST2]
go
create table [TEST2]([D] varchar(2),[E] datetime,[F] int)
insert [TEST2]
select 'F0','2009-9-1',4 union all
select 'F0','2009-9-3',4 union all
select 'F0','2009-9-4',4 union all
select 'F6','2009-9-2',5
--------------开始查询--------------------------
select
isnull(a,d) ,isnull(b,e) ,isnull(c,1)*isnull(f,1)
from
test1 a
full join test2 b on
a.a=b.d and a.b = b.e
----------------结果----------------------------
/* ---- ----------------------- -----------
F0 2009-09-01 00:00:00.000 8
F0 2009-09-02 00:00:00.000 2
F0 2009-09-04 00:00:00.000 8
F6 2009-09-01 00:00:00.000 3
F0 2009-09-03 00:00:00.000 4
F6 2009-09-02 00:00:00.000 5

(6 行受影响)

*/
昵称被占用了 2009-09-15
  • 打赏
  • 举报
回复
要用full join的
昵称被占用了 2009-09-15
  • 打赏
  • 举报
回复
select 
isnull(a,d) as ad
,isnull(b,e) as be
,isnull(c,1)*isnull(f,1) as cf
from test1 a full join test2 b
on a.a=b.d and a.b = b.e
ming_Y 2009-09-15
  • 打赏
  • 举报
回复
select *,test1.c*test2.f as a from test1,test2 where test1.a=test2.d and test1.b=test2.e
jinjazz 2009-09-15
  • 打赏
  • 举报
回复
select test1.a,test1.b,test1.c*test2.f from 
test1 inner join test2
on test1.a=test2.d and test1.b=test2.e
翼帆 2009-09-15
  • 打赏
  • 举报
回复
select A,C*F from test1,test2 where A=D and B=E 

是这意思?
jinjazz 2009-09-15
  • 打赏
  • 举报
回复
select a.a,a.b,a.c*b.f from test1 a,test2 b
where a.a=b.d and a.b=b.e

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧