问题
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?
回答1:
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
回答2:
The limit option in mysql can take two parameters, start,length. Therefore you can do:
limit 0,5
to get the first 5
limit 5,5
to get the next 5
limit 10,5
to get the next etc. You just need to set the first number based on your lastOutputId.
回答3:
I agree with the answers above, LIMIT takes either 2 parameters or 1:
- SELECT ... LIMIT offset num_records
- SELECT ... LIMIT num_records --> equivalent to LIMIT 0 num_records
I do warn against this approach for performance reasons though.
An SQL SELECT statement goes through all MySQL server software layers (connection, auth, query parsing, query result cache, storage engine, results serialization), every time your web site wants 5 lines.
SELECT something FROM somewhere LIMIT 1000000,10 will actually read 1,000,010 records from the tables, causing big load on the storage engine! The first 1M lines are just ignored in the results serialization.
It may be more efficient to get the whole data in one query and to keep the results in local memory on the web server or in a shared volatile storage like memcached.
回答4:
class celeb {
public function getAllCelebDataFinal ($database,$celebrityId,$lastOutputId, $limit)
// Your query
}
}
$numberOfRepeat = 5; //just example
$limit = new Celeb();
$limit->getAllCelebDataFinal($val, $val, $val, $val);
}
Then put the object in a foreach loop and make that number dynamic through a button or a get variable and you are done :)
回答5:
$limit = $_REQUEST['limit'];
$start = $_REQUEST['start'];
$getRec = "SELECT * FROM MyUser LIMIT $start , $limit";
来源:https://stackoverflow.com/questions/15681092/how-to-change-limit-dynamically-in-mysql