CREATE TABLE [dbo].[test](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[ctext] [varchar](12) NOT NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED
(
[id] ASC
))
CREATE TABLE [dbo].[Comments](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[cComments] [varchar](250) NOT NULL,
[fk_test] [bigint] NOT NULL)
GO
ALTER TABLE [dbo].[Comments] WITH CHECK ADD CONSTRAINT [FK_Comments_test] FOREIGN KEY([fk_test])
REFERENCES [dbo].[test] ([id])
ON DELETE CASCADE
两个表与一个正常的一对多关系链接。Sql:从两个表中选择数据而不重复第一个表值?
我想选择返回一个列表的列表:
-----------------------------------------------------
|test.id|test.cnumber|comments.id|comments.cComments|
|-------|------------|-----------|------------------|
| 1 | mytest1 | 1 | comment1 |
| | | 2 | comment2 |
| | | 3 | comment3 |
| | | 4 | comment4 |
| | | 5 | comment5 |
| | | 6 | comment6 |
| 2 | mytest2 | 7 | comment7 |
| | | 8 | comment8 |
| 3 | mytest3 | 7 | comment9 |
-----------------------------------------------------
反正是有办呢?
即时通讯尝试用sql存储过程替代linq。
也许你应该这样做在应用层。在oracle中 – 2013-04-10 12:17:36
,你会用一个LAG函数 – Randy 2013-04-10 12:35:13
哪个SQL Server版本? 2012还支持'lag()'函数。 – 2013-04-10 12:44:08