2010-01-19 4 views

Répondre

1

SELECT TOP ... avec une clause where appropriée et/ou join devrait faire ce que vous cherchez; il y a lots of questions and answers sur son utilisation ici sur SO.

Exemple:

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 

retourne deux lignes:

'three' 
'two' 
0

Pas assez de détails et voir plus de ce que vous avez essayé contribuerait à la construction d'une meilleure réponse, mais si un est la table parente et b l'enfant ...

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 
Questions connexes