简体   繁体   中英

ms access 2003 random record within join

I am trying to get a random record from a join, but I cannot get it to work? Any ideas. It's using Microsoft Access 2003.

table = members
===============
memberID int
name varchar

table = testimonials
====================
testimonialID int
memberID int
content memo

select m.*, (SELECT t.content from testimonials t where t.memberID=m.memberID ORDER BY rnd(t.testimonialID)) as testimonialtext
FROM members m;

I basically need each member record and one random testimonial for that member. I should also add that I've tried it within a join too...

select m.*, t.content FROM members m
INNER JOIN testimonials t ON m.memberID=t.memberID
ORDER BY rnd(t.testimonialID)

Thanks for any help.

try to limit with TOP clause

select m.*, (SELECT top 1 t.content from testimonials t where t.memberID=m.memberID ORDER BY rnd(t.testimonialID)) as testimonialtext
FROM members m;

ok, then we can do something like:

select 
  m.*, 
  t3.content
FROM 
  members m, 
   (select 
      top 1 t.testimonialID, 
    from 
      testimonials t 
    where 
      t.memberID=m.memberID 
    order by 
      rnd(t.testimonialID)
    ) t2,
   testimonials t3 
where
  t3.testimonialID = t2.testimonialID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM