PDA

View Full Version : Giúp dùm câu Select JOIN



Cận
05-12-2003, 19:02
EM có 2 table cha và con ( Parent & Children )

Bây giờ em muốn làm 1 câu Select, với mỗi record ở table cha ( Parent ), nó sẽ hiển thị tương ứng 1 ( chỉ 1 ) record tương ứng ở table con ( Children ) và record này có ID cao nhất. Em là như sau:

SELECT Parent.ID, Parent.Text, Children.ID, Children.Text from Parent
LEFT JOIN Children
ON Parent.ID = Children.ParentID
WHERE Children.ID = (select max(ID) from Children where Children.ParentID = Parent.ID)


Đó, nhưng mà khi chạy, gặp record nào bên Parent không có record tương ứng bên Children thì nó không hiện ra :bawling: . Bây giờ em muốn nó hiện ra NULL hết thì sao ?

tcm1901
05-12-2003, 22:22
try this...

SELECT Parent.ID, Parent.Text, Children.ID, Children.Text from Parent
LEFT JOIN Children
ON Children.ID = (select max(ID) from Children where Children.ParentID = Parent.ID)

:)

Cận
06-12-2003, 17:43
Thanks nhiều. Chạy tốt lắm :)