【难】求一SQL查询语句
下面是创建测试数据的脚本,请copy到 SQL Query Analyzer中查看。
----------------------------------
use pubs
if exists (select * from dbo.sysobjects where id = object_id(N '[dbo].[Orders] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1)
drop table [dbo].[Orders]
GO
if exists (select * from dbo.sysobjects where id = object_id(N '[dbo].[Orders_Child1] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1)
drop table [dbo].[Orders_Child1]
GO
CREATE TABLE [dbo].[Orders] (
[OrderID] [int] NOT NULL ,
[OrderCode] [varchar] (10) NOT NULL ,
[OrderCount] [int] NULL ,
[OrderRemark] [nvarchar] (50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Orders_Child1] (
[ChildID] [int] NOT NULL ,
[OrderID] [int] NOT NULL ,
[Date1] [smalldatetime] NULL ,
[Number1] [int] NULL ,
[Text1] [nvarchar] (20) NULL ,
[Text2] [nvarchar] (20) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders] WITH NOCHECK ADD
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders_Child1] WITH NOCHECK ADD
CONSTRAINT [PK_Orders_Child1] PRIMARY KEY CLUSTERED
(
[ChildID]
) ON [PRIMARY]
GO
insert into orders
select 1, 'A01 ',10000, 'A01文本备注 '
union select 2, 'A02 ',20000, 'A02文本备注 '
union select 3, 'A03 ',25000, 'A03文本内容 '
insert into orders_Child1
select 1,1, '2007-01-01 ',1000, 'A01的文本1 ', '齐 '
union select 2,1, '2007-01-02 ',2000, 'A01的文本2 ', '齐 '
union select 3,1, '2007-01-03 ',2000, 'A01的文本3 ', '齐 '
union select 4,2, '2007-01-04 ',100, 'A02的文本1 ', '不齐 '
union select 5,2, '2007-01-05 ',200, 'A02的文本2 ', '不齐 '
union select 6,2, '2007-01-06 ',200, NULL , '不齐 '
union select 7,3, '2007-01-07 ',10000, 'A03的文本1 ', '齐 '
union select 8,3, '2007-01-08 ',NULL , NULL , '不齐 '
----------------------------------
select * from Orders
select * from Orders_Child1
想得到的结果:
OrderID OrderCode OrderCount OrderRemark Date1