Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php / Mysql Union and Limit offset

I have 3 tables with the exact same structure and I need to query them as one big table, order and limit this big table as a whole. The limit is an offset as I use a pagination.

This is what I have so far:

$from = (($page_number-1)*10);
$to   =  ($page_number)*10;     

$request = mysql_query("
    (SELECT * FROM table_a) 
    UNION ALL 
    (SELECT * FROM table_b) 
    UNION ALL 
    (SELECT * FROM table_c) 
    ORDER BY title ASC 
    LIMIT ".$from.",".$to 
);

$z=0;

while ($result = mysql_fetch_array($request)) 
{
    ....
    $z++;
};

$counter = $z;

I expect the $counter to be equal to 10, whatever the page but:

On page 1, $counter = 10

On page 2, $counter = 20

On page 3, $counter = 23

On page 4, $counter = 3

Ok if it's not necessarily equal to 10 for the last page because I get what's left from the list but getting 20 and 23 for page 2 and page 3 doesn't make any sens to me.

It's has to be related to the LIMIT because, if I only use a single table in a classic way, my counter is always equal to 10 (unless it's the last page, of course).

What's wrong here?

Thanks!

like image 384
Baylock Avatar asked Feb 04 '14 23:02

Baylock


People also ask

Can you use LIMIT with union?

ORDER BY, LIMIT, and OFFSET Clauses in UNIONEach SELECT statement in a UNION clause can specify its own ORDER BY , LIMIT , and OFFSET clauses. In this case, the SELECT statement must be enclosed by parentheses.

What is the difference between LIMIT and offset in MySQL?

MySQL Offset is used to specify from which row we want the data to retrieve. To be precise, specify which row to start retrieving from. Offset is used along with the LIMIT. Here, LIMIT is nothing but to restrict the number of rows from the output.

What is the difference between LIMIT and offset?

The limit option allows you to limit the number of rows returned from a query, while offset allows you to omit a specified number of rows before the beginning of the result set. Using both limit and offset skips both rows as well as limit the rows returned.

Why ORDER BY with LIMIT and offset is slow?

Why OFFSET is so slow? Well, in most cases, low offset queries are not slow. The problem starts with high OFFSET values. If your query is using the following limit clause: “LIMIT 50000, 20”, it's actually requesting the database to go through 50,020 rows and throw away the first 50,000.


1 Answers

Limit should have the start and the number of rows you want. So 0, 10 if you want the first ten, and then 10, 10 if you want the next 10 (not 10, 20 which will give you 20 rows starting at row 10).

If you are still having issues, try putting the selections and unions in their own set of (). I'm not sure about the order of operations, it is possible your limit is only applying to the last table.

$request = mysql_query("
    ( 
    (SELECT * FROM table_a) 
    UNION ALL 
    (SELECT * FROM table_b) 
    UNION ALL 
    (SELECT * FROM table_c)
    ) 
    ORDER BY title ASC 
    LIMIT ".$from.",10
);
like image 157
Damien Black Avatar answered Oct 02 '22 10:10

Damien Black