The Prophet ﷺ said, "A time will come when groups of people will go out to fight, and it will be asked, 'Is there among you anyone who accompanied the Prophet ﷺ?' The answer will be, 'Yes,' and victory will be granted to them. Then another time will come, and it will be asked, 'Is there among you anyone who accompanied the companions of the Prophet ﷺ?' The answer will be, 'Yes,' and victory will be granted. Then another time will come, and it will be asked, 'Is there among you anyone who accompanied a companion of the companions of the Prophet ﷺ?' The answer will be, 'Yes,' and victory will be granted."
عن النبي ﷺ قال: يأتي زمان يغزو فئام من الناس، فيقال: فيكم من صحب النبي ﷺ؟ فيقال: نعم، فيفتح عليه، ثم يأتي زمان فيقال: فيكم من صحب أصحاب النبي ﷺ؟ فيقال: نعم، فيفتح، ثم يأتي زمان فيقال: فيكم من صحب صاحب أصحاب النبي ﷺ؟ فيقال: نعم، فيفتح.
Bukhari narrated it in the Book of Jihad and Expeditions (2897), and Muslim in the Book of the Virtues of the Companions (2532-208), both through routes from Sufyan bin Uyayna, who said, Amr heard Jabir reporting from Abu Saeed Khudri, and he mentioned it. The wording is Bukhari's, and Muslim related it similarly.