How to change Limit dynamically in mySQL


Problem :

public function getAllCelebDataFinal ($database,$celebrityId,$lastOutputId) 
{
    $getalldata = $database->executeObjectList("
        SELECT *
        FROM (
            (SELECT v.vidId, NULL as newsId, NULL as photoId, NULL as imageFile,
                    v.title as vidTitle, NULL as newsTitle, v.videoLink AS vidLink,
                    NULL as newsVidLink, NULL as newDetail , v.addDate
             FROM videos v
             WHERE v.celebrityId='".$celebrityId."' AND v.isPublished=1)
          UNION ALL
            (SELECT NULL as vidId, n.newsId,NULL as photoId,NULL as imageFile,
                    NULL as vidTitle, n.title as newsTitle, NULL AS vidLink,
                    n.videoLink as newsVidLink, n.details as newDetail,
                    n.addDate
             FROM news n
             WHERE n.celebrityId='".$celebrityId."' AND n.isPublished=1)
          UNION ALL
            (SELECT NULL as vidId,NULL as newsId,p.photoId,p.imageFile,
                    NULL as vidTitle, NULL as newsTitle, NULL AS vidLink,
                    NULL as newsVidLink,  NULL as newDetail , p.addDate
             FROM photos p
             WHERE p.celebrityId='".$celebrityId."' AND p.isPublished=1)
        ) results
        ORDER BY addDate DESC
        LIMIT 5");
    return $getalldata; 
}

Consider it as a second query, if i am running first query with the limit 5, i am sendind 5 as $lastOutputId as a parameter, and i want to run this query as 5 to 5+5, and in the same way, next query would run with limit 10 to 15. How can i do that?



Solution :

LIMIT may have 2 parameters. In our case I think you can use LIMIT like this:

LIMIT $lastOutputId, 5

Doc: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm


Mysql Tutorials

Mysql Howto..

How to handle a n:m relation correctly with PHP&MySQL?

How to store European Currency in MySQL?

how to insert time as string by time intervals into mysql database

how to solve Subquery returns more than 1 row error in multiple selection

How to find which page something starts on in a numbered pagination of sorted results?

Show progress of filling a DataSet from MySQL

How to get write 'where' condition using 'am' in mysql.

Mysql YEAR how to set the current year as default value

How to group MySQL query by multiple criterias?

How to improve Limit clause in MySQL

how to grant all user using mysql server

how to run a mysql query in yii [closed]

How to Insert a datetime in the future in PHP/mysql?

Using MySQL in Powershell, how do I pipe the results of my script into a csv file?

Re-installing MYSQL(windows) - How to get to the old data with the new install?

Create a website to show data from a remote XML dataset

How to insert data from a tornado web-socket to a mySQL database using Django

How to recover/Change MySQL Password

How to search full-text mysql field in order to return only a sentence when the keyword is found (not to return the whole field)

How can I show the counts of distinct values and include zeros?

How to make MySQL order values by BigInteger instead of varchar when column to order by is in case statement?

How to retrieve a count number of specified values in mysql (even if no records)?

How to scroll through a count query in MySQL/PHP

How to avoid multiple column in correlated sub query assignment in MySQL update

MYSQL SELECT : How to include a field from another table?

How To Skip Some Fields From Database While Using fputcsv

how to use VC to operate the date in files of MYSQL

How do I fix the 'Out of range value adjusted for column' error?

How to join in mysql

how can add auto watermark script in my news site?