php - 在分页限制中使用联合
问题描述
请有人可以通过“联合”选择语句的分页来帮助mysql查询。使用页面限制和偏移限制
如果我弄错了,下面是我的 sql。
$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). "";
这是 pdo 中的完整 sql 代码
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;
}`
解决方案
尝试这个
$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). "";
推荐阅读
- java - 如何使用 Stream API 按日期对列表进行分组以映射?
- php - JSON POST Empty 使用 cURL 命令
- javascript - 使用正则表达式查找对象的值
- bash - 在保留行组的同时拆分文件
- coq - 如何在 Coq 中重写给定的两个依赖类型相等
- git - git 是否有一个命令可以将工作树重置为 HEAD 但保留索引的分阶段更改?
- dictionary - 将值插入地图时出现恐慌/错误
- virtual-machine - Virtualbox 没有端口转发端口 8080 但其他端口都可以
- kubernetes - 使用一个 API 请求从节点中删除特定的污点
- excel - MS Access VBA 正在创建多个报告电子邮件,并且参数未存储表单中的引用