please can someone help with mysql query with pagination for "union" select statement.using page limit and offset limit
below is my sql if am getting it wrong.
$sqlQuery = "SELECT title, content, img, postlink, created_at, 'forum' as type FROM forum_post WHERE title LIKE CONCAT('%', :keyword, '%')
UNION SELECT title, content, img, postlink, created_at, 'music' as type FROM music_post WHERE title LIKE CONCAT('%', :keyword, '%') UNION
SELECT title, content, img, postlink,created_at, 'video' as type FROM video_post WHERE title LIKE CONCAT('%', :keyword, '%') ORDER BY created_at DESC LIMIT ".($lower_limit)." , ". ($page_limit). "";
here is the full sql code in pdo
here is the full sql query `public function all_search($keyword, $lower_limit, $page_limit) {
$sqlQuery = "SELECT * FROM(SELECT title, content, img, postlink, created_at, 'forum' as type FROM forum_post WHERE title LIKE CONCAT('%', :keyword, '%')
UNION SELECT title, content, img, postlink, created_at, 'music' as type FROM music_post WHERE title LIKE CONCAT('%', :keyword, '%') UNION
SELECT title, content, img, postlink,created_at, 'video' as type FROM video_post WHERE title LIKE CONCAT('%', :keyword, '%')) tab LIMIT ".($lower_limit)." , ". ($page_limit). "";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute(['keyword'=>$keyword]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}`
Try this
$sqlQuery = "SELECT * FROM(SELECT title, content, img, postlink, created_at, 'forum' as type FROM forum_post WHERE title LIKE CONCAT('%', :keyword, '%')
UNION SELECT title, content, img, postlink, created_at, 'music' as type FROM music_post WHERE title LIKE CONCAT('%', :keyword, '%') UNION
SELECT title, content, img, postlink,created_at, 'video' as type FROM video_post WHERE title LIKE CONCAT('%', :keyword, '%'))tab LIMIT ".($lower_limit)." , ". ($page_limit). "";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.