Each SELECT statement that does not insert into a table or a variable will produce a result set.
If you want your stored procedure to return only one result set, make sure that you only have one SELECT statement. If you have other SELECT statements, make sure that they insert results into a table or variable.
UPDATE
Here are examples of stored procedures.
This stored procedure would return one result set:
DELIMITER ;;
CREATE DEFINER=CURRENT_USER PROCEDURE stored_procedure_name()
BEGIN
DECLARE local_variable_name INT;
SELECT column_name FROM table_1 LIMIT 1 INTO local_variable_name;
SELECT * FROM table_1;
END;;
DELIMITER ;
This stored procedure would return two result sets:
DELIMITER ;;
CREATE DEFINER=CURRENT_USER PROCEDURE stored_procedure_name()
BEGIN
DECLARE local_variable_name INT;
SELECT column_name FROM table_1 LIMIT 1 INTO local_variable_name;
SELECT * FROM table_1;
SELECT * FROM table_2;
END;;
DELIMITER ;
I suppose you could do something like this from the command line/phpmyadmin:
Assuming the following table:
mysql> SELECT * FROM test;
+----+----------+
| id | cat_type |
+----+----------+
| 1 | free |
| 2 | free |
| 3 | free |
| 4 | free |
| 5 | paid |
| 6 | paid |
+----+----------+
6 rows in set (0.00 sec)
Make your custom ordering by selecting twice the table and using a variable as a counter:
mysql> SET @i := 0;
SET @j := 0.5;
SELECT id, cat_type FROM
(SELECT @i := @i + 1 as ordering, id, cat_type FROM test WHERE cat_type = 'free'
UNION
SELECT @j := @j + 2 as ordering, id, cat_type FROM test WHERE cat_type = 'paid')
AS base_table
ORDER BY ordering;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+----+----------+
| id | cat_type |
+----+----------+
| 1 | free |
| 2 | free |
| 5 | paid |
| 3 | free |
| 4 | free |
| 6 | paid |
+----+----------+
6 rows in set (0.00 sec)
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:
+----+-------------------+------------+------+---------------+------+---------+------+------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------------+------------+------+---------------+------+---------+------+------+-----------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 12 | Using filesort |
| 2 | DERIVED | test | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
| 3 | UNCACHEABLE UNION | test | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+-------------------+------------+------+---------------+------+---------+------+------+-----------------+
4 rows in set (0.00 sec)
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.
Best Answer
I think this query will work for you :