2 years ago

#72217

test-img

nikviz

How to equally split the results of MYSQL?

Let's say I write a SELECT statement in MYSQL(version MySQL 8.0) which fetches "n" records.
I want this "n" records be split approximately equally by 3 and additionally need to make sure that 10% of these n records are shared by the 3 parts that it was divided into.

Is it possible to do this in the select statement itself or do I need to write a function to perform this operation.

Example: My select query fetches as below:

ID Name Age
1  ABC  22
2  XYZ  23
3  awe  22
4  wer  23
5  per  23
6  mnm  24
7  lmk  23
8  uyt  22
9  prt  23
10 ler  26

I now need it split into let's say 3 and 10% should be shared by the 3 parts that it has been split to (since it's 10 records, in this example 1 record should be shared by the three sets)

Set 1:
1  ABC  22
2  XYZ  23
3  awe  22
4  wer  23
Set 2:
4  wer  23
5  per  23
6  mnm  24
7  lmk  23
Set 3:
4  wer  23
8  uyt  22
9  prt  23
10 ler  26

mysql

sql

select

0 Answers

Your Answer

Accepted video resources