如果我从表1
中选择posts
帖子
加(联合)11
帖子表格parchive
最终的数字不是12而是11:
$stmt = $db->query("SELECT * FROM posts where status='public' order by inde asc limit 12 offset " . $offset);
$count1 = $stmt->rowCount(); // echo: 1
if ($count1 < 12){
$diff = 12 - $count1;
$stmt = $db->query("(select * from posts where status='public' order by inde asc limit 12 offset " . $offset . ")
union (select * from parchive where status='public' order by date desc limit " . $diff .")");
$count2 = $stmt->rowCount(); // echo 11
}
因此,如果$count1
为1
且$diff
为11
,则
$count2
应该是12 (1+11)
而不是11
!
答案 0 :(得分:1)
如果您需要所有结果而不删除重复值,请使用union ALL
$stmt = $db->query("(select *
from posts where status='public' order by inde asc limit 12 offset " . $offset . ")
union all (select *
from parchive where status='public' order by date desc limit " . $diff .")");