select union不会总结选择

时间:2017-07-09 17:53:53

标签: php mysql

如果我从表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
    }

因此,如果$count11$diff11,则 $count2应该是12 (1+11)而不是11

1 个答案:

答案 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 .")");