I have two tables where the data is not related
For each row in table A i want e.g. 3 random rows in table B
This is fairly easy using a cursor, but it is awfully slow
So how can i express this in single statement to avoid RBAR ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To get a random number between 0 and (N-1), you can use.
Which means to get positive values 1-N, you use
Note:
RAND()doesn’t work – it is evaluated once per query batch and you get stuck with the same value for all rows of tableA.The query:
(assuming you wanted up to 9 random rows of B per A)