开发者

How Select top row from child table [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be r开发者_StackOverflow社区easonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

i have two tables and i want to select top row from child table? there are relation by two tables by id. what can i do please help me????


SELECT TOP ... with an appropriate where clause and/or join should do what you're looking for; there are lots of questions and answers about its use here on SO.

Example:

CREATE TABLE a (pid int, tstamp datetime)
go
CREATE TABLE b (fid int, foo varchar(254))
go

INSERT INTO a (pid, tstamp) VALUES (1, '2010-01-12')
INSERT INTO a (pid, tstamp) VALUES (2, '2010-01-02')
INSERT INTO a (pid, tstamp) VALUES (3, '2010-01-01')
INSERT INTO a (pid, tstamp) VALUES (4, '2010-01-24')

INSERT INTO b (fid, foo) VALUES (1, 'one')
INSERT INTO b (fid, foo) VALUES (2, 'two')
INSERT INTO b (fid, foo) VALUES (3, 'three')
go

SELECT TOP 2 b.foo
FROM a
INNER JOIN b ON a.pid = b.fid
ORDER BY a.tstamp

Returns two rows:

'three'
'two'


Not enough details and seeing more of what you've tried would help with building a better answer, but if a is the parent table and b the child...

SELECT TOP 1 b.*
FROM b
JOIN a ON (a.ID = b.ID)
WHERE a.ID = someParentID
AND b.SomeField = "someChildCriteria"
ORDER BY b.ID DESC  -- or by whatever field needs to be topmost
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜