I am trying to get a sorted result from a MySQL query and I don't have any idea of making the query.
I have a example MySQL table as follows :
id | cat_type
1 | free
2 | free
3 | free
4 | free
5 | paid
6 | paid
... | ...
... | ...
... | ...
and it goes on.
The result should be as follows from the above table :
id | cat_type
1 | free
2 | free
5 | paid
3 | free
4 | free
6 | paid
... | free
... | free
... | paid
and goes on(should be sorted)
The total result should be split as 3. In the first three result, first two should be free
and the next one should be paid
likewise all the remaining result should be sorted as two free
and one paid
.
The most important is that it should be sorted as two free
and one paid
.
Any idea how to accomplish this using a query?
Best Answer
I suppose you could do something like this from the command line/phpmyadmin:
Assuming the following table:
Make your custom ordering by selecting twice the table and using a variable as a counter:
This is technically incorrect, as it could explore the original tables not in id order, but assuming you are using InnoDB, a full table scan in index order should be performed.
Also, this is terribly unoptimized:
To the point that if you wanted to do this in a large table, doing it programatically or using cursors would be a better idea.