I wish to achieve something like this from a MYSQL SELECT query
I wish to group the results from the select in 500 rows (per batch) and have a column called group (or anything) for the key to the group (eg bartch 0 1 2 3)
SELECT * TABEL WHERE X
Would I do the select into a temp table with the a column-indexed then some how do a limit of 500 and move forward?
The reason behind this is so I can call the number again to get all the same rows again.
Or would it be a better idea to just to select everything thing and let the PHP or Python do the work?
+----+------------+
| Group | Name
+------------------
| 1 | Ham
| 1 | Bread
+------------------
| 2 | Tomato
| 2 | Eges
SELECT * FROM TABLE
2
Answers
The idea is to first calculate the data belonging to the same group and then group by the group number. Since I don’t know what will be done after grouping, I will use concatenating the grouped strings as an example.Using ROW_NUMBER() generates a row number, and by subtracting 1 and dividing by the number of rows per group, we calculate the group number, applying the FLOOR() function to round down in the outer layer.Because x/x = 1, if the row number starts from 1, rounding down will result in the number of groups being one less than needed (for example: 1 to (x-1) will be batch 1, and x to (2x-1) will be batch 2…).Therefore, we need to subtract "1 from the row number" to achieve the effect of grouping as 1 to x.
The main issue here could be this:
To be sure to fetch the same rows of groups (0, 1, 2, …) it is essencial to have the same and consistent order by to define your groups. It is not clear from the question do you have such a column(s) to do the ordering consistently.
1. table data with ID column to be used in Order By (consistently)
… if you add a new row with a new id …
… and run the SQL again the new row (8, ‘Beans’) will be added at the end, but all previous group data willl remain the same …
2. table data without a column to be used in Order By (consistently)
S a m p l e D a t a :
… if you don’t order your data there could be different arbitrary ordering whenever you run the code … so lets order it by food
R e s u l t :
… now if you add a new row with a new food …
… and run the SQL again the new row (‘Beans’) will change the group content where it was inserted and all following groups content …
fiddle