MySQL, How get from query (who use LIMIT) number of returned rows and save rowset. (Store procedure / function) and by number of rowset statement

StackOverflow https://stackoverflow.com/questions/13891865

Pregunta

For example Table x have 9 records

x = 5

I make query:

 Select * From `x` LIMIT 0,5

// I need data from this query and rowset count.

// Variable count -- Stores the number of rows returned by first query,

When first query rowset is smaller than 5 rows, then

I make new query from table z

  Select * From `z` Limit 0,(5 - count)

  --------------------------------

Trick by FOUND_ROWS not working.

http://pastebin.com/1kKD0wqC

  --------------------------------

Problems:

How to do it in Store procedure / function (MySQL)?

How get from first query (rowset and number of returned rows) in one query?

  --------------------------------

Targets:

After work function should return

Sum of rowset (query 1 and 2).

Or

rowset query 1

¿Fue útil?

Solución 2

I was found solution:

MySQL (Content of Store Procedure):

Set @tmp = 0;
Select `field_1`, `field_2` From `table_1` Where @tmp := @tmp + 1 LIMIT 0 ,5;

// We have row set and in session variable (@tmp) - number of returned rows


@TIP

 @tmp := @tmp + 1

It's Must be before all conditions in where.

For example:

[.....] Where @tmp := @tmp + 1 And `field_2` > 1 LIMIT 0, 5;

Will return always 1...


Correct Version

[.....] Where `field_2` > 1 And  [Other conditions] And @tmp := @tmp + 1  LIMIT 0, 5;

Otros consejos

Select * From `z` Limit 0,(5 - (SELECT COUNT(*) FROM `x` LIMIT 0,5))

Didn't check if it works but it sholud. I refer to the manual: http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top