开发者

SQL LIMIT for multiple queries

I have my paging script that gets the page variable and puts in in LIMIT values inside a sql query.

LIMIT $page, $limit2 

it all works well,the problem is that i have 2 sql queries so LIMIT 0,10 displays 10 results from first query and then 10 more from second query =20 results per page,what i need is to display 10 from 1st QRY and on 2nd page the rest from first QRY and only then to print the results from 2nd query.

I tried if statement before whole query an a ++increment inside while loop to even the numbers with found results and then run 2nd QRY but it seems that it doesn't work that way.

also tried something like this with few matematical operations but it also doesn'开发者_如何学Ct do much good.

    if($page==$printf2)
    {$limit2=10;} 
    if($page==0)
    {$limit2=0;}

What would be the proper way of making the 2nd QRY list after 1st one ended,without more than 10 results per page?


did you evaluate to use UNION statement?

SELECT * FROM xxxxx (first query) 
UNION ALL
SELECT * FROM YYYYYY (second query)


$sql = "QUERY1 LIMIT $page, $limit";
$result = mysql_query($result);

/* process the result here! */

if (mysql_num_rows($result) < 10) {
    $sql2 = "QUERY2 LIMIT $page, $limit2";
    $result = mysql_query($result);

    /* process result of query 2 */
}

This way, the query two is executed only when the rows returned from query1 is the last set of rows in the table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜