Click here to Skip to main content
16,011,870 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi...

I have Table as Id is unique
Id Quantity
1 1
2 1
3 2
4 1
5.. 3..

I want to select Id's whose sum(Quantity)=10 randomly in MYSQL can anyone help?
Posted

1 solution

Simply select all records that have a sum of 10. Then simply add this to the query to get 10 random results from it.
ORDER BY rand() LIMIT 10

Beware it isn't the most optimal query because first it figures out the rows having a sum of 10, then order them by random (meaning non-deterministic) and eventually only take 10 from that. But... it does the job.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900