如何将两个查询合并在一起以显示具有分页的一组数据

时间:2018-06-22 13:15:03

标签: php mysql wordpress

现在更新,我想我几乎在那里...:-)

嘿,下面的代码,我在滚动页面时发现重复的帖子。

我认为我的代码的分页方面可能有问题。如果不是这样,那可能是它的MYSQL方面。会有更好的方法吗?任何帮助将不胜感激。

 <div class="all box">
  <?php
function offers_current( ){
    global $wpdb, $paged, $max_num_pages, $current_date;

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $post_per_page = 1;
    $offset = ($paged - 1) * $post_per_page;

    //if($paged == 1){
    //  unset($_SESSION['seed']); 
//  }
    $seed = $_SESSION['seed'];
    if (empty($seed)) {
      $seed = rand();
      $_SESSION['seed'] = $seed;
    }

    $orderby_statement = 'RAND('.$seed.')';





 $querystr2 = "
  SELECT SQL_CALC_FOUND_ROWS wp_posts.*, wp_geodir_gd_hotels_detail.*
FROM wp_posts 
INNER JOIN wp_geodir_gd_hotels_detail
ON (wp_geodir_gd_hotels_detail.post_id = wp_posts.ID) 
WHERE 1=1 
AND ( wp_posts.post_status = 'publish' 
OR wp_posts.post_status = 'private' )
AND wp_posts.post_type = 'gd_hotels'
AND wp_geodir_gd_hotels_detail.is_featured = '1'
AND ( wp_geodir_gd_hotels_detail.geodir_special_offers != ''
AND wp_geodir_gd_hotels_detail.geodir_special_offers IS NOT NULL ) 
GROUP BY wp_posts.ID 
";

 $pageposts2 = $wpdb->get_results($querystr2, OBJECT);



 $querystr3 = "
  SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, wp_geodir_gd_arts_detail.*
FROM wp_posts 
INNER JOIN wp_geodir_gd_arts_detail
ON (wp_geodir_gd_arts_detail.post_id = wp_posts.ID) 
WHERE 1=1 
AND ( wp_posts.post_status = 'publish' 
OR wp_posts.post_status = 'private' )
AND wp_posts.post_type = 'gd_arts'
AND wp_geodir_gd_arts_detail.is_featured = '1'
AND ( wp_geodir_gd_arts_detail.geodir_special_offers != ''
AND wp_geodir_gd_arts_detail.geodir_special_offers IS NOT NULL ) 
GROUP BY wp_posts.ID 

 ";

 $pageposts3 = $wpdb->get_results($querystr3, OBJECT);


 $querystr4 = "
  SELECT SQL_CALC_FOUND_ROWS wp_posts.*, wp_geodir_gd_attractions_detail.*
FROM wp_posts 
INNER JOIN wp_geodir_gd_attractions_detail
ON (wp_geodir_gd_attractions_detail.post_id = wp_posts.ID) 
WHERE 1=1 
AND ( wp_posts.post_status = 'publish' 
OR wp_posts.post_status = 'private' )
AND wp_posts.post_type = 'gd_attractions'
AND wp_geodir_gd_attractions_detail.is_featured = '1'
AND ( wp_geodir_gd_attractions_detail.geodir_special_offers != ''
AND wp_geodir_gd_attractions_detail.geodir_special_offers IS NOT NULL ) 
GROUP BY wp_posts.ID 


ORDER BY ".  $orderby_statement ."
        LIMIT ".$offset.", ".$post_per_page." 
 ";



 $pageposts4 = $wpdb->get_results($querystr4, OBJECT  );

     $sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" );
    $max_num_pages = ceil($sql_posts_total / $post_per_page);
   $super_merge = array_merge(   $pageposts2,  $pageposts3,  $pageposts4 );



 return  $super_merge;
}


      global $post; 


    $offers_current = offers_current();



 foreach (    $offers_current as $post2): 



  if(isset($post2->geodir_special_old) && $post2->geodir_special_perc > 0){

    $final_perc = $post2->geodir_special_perc .'% OFF';

} 

else {
    $final_perc = '';
}

setup_postdata($post2); ?>
    <div class="latest-offer-box-big">
  <div class="latest-offer-box">
    <?php 


$image_hotel = $post2->geodir_logodetail;
$image_new_hotel = $post2->geodir_special_image;

if(isset($post2->geodir_special_image) && $post2->geodir_special_image > ''){
    ?>
    <div id="super-backimage" style="background:url(<?php echo $image_new_hotel ?>)"> </div>
    <?php 
} 

else { ?>
    <div id="super-backimage" style="background:url(<?php echo $image_hotel ?>)"> </div>
    <?php }
?>
    <h4 style="width:100%; float:left; margin-bottom:0;">
      <?php echo get_the_title($post2); ?>
    </h4>
    <span id="spec_tit"><?php echo  $post2->geodir_special_title ?></span>
    <div id="super-view-offer"> <a id="s-link" href="<?php the_permalink($post2);?>">View Offer</a>
      <div id="super-price"> <span style="text-decoration: line-through;color:#666; font-weight:300"><?php echo $post2->geodir_special_old ?> </span> <?php echo $post2->geodir_special_price ?><br>
        <span  style="color:#000; font-size:15px;"><?php echo $final_perc;  ?></span> </div>
    </div>
  </div>
     </div>

  <?php  







   endforeach; ?>









  <div class="navigations" style="width:100%; float:left;">
    <div class="previous panel"><?php previous_posts_link('&laquo; previous',$max_num_pages) ?></div>
    <div class="next panel"><?php next_posts_link('Next &raquo;',$max_num_pages) ?></div>
</div>







</div>

3 个答案:

答案 0 :(得分:1)

在联合/联合中,所有查询,排序和限制必须放在末尾

$sql1 = "
              SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, wp_geodir_gd_arts_detail.*
            FROM wp_posts 
            INNER JOIN wp_geodir_gd_arts_detail
            ON (wp_geodir_gd_arts_detail.post_id = wp_posts.ID) 
            WHERE 1=1 
            AND ( wp_posts.post_status = 'publish' 
            OR wp_posts.post_status = 'private' )
            AND wp_posts.post_type = 'gd_arts'
            AND wp_geodir_gd_arts_detail.is_featured = '1'
            AND ( wp_geodir_gd_arts_detail.geodir_special_offers != ''
            AND wp_geodir_gd_arts_detail.geodir_special_offers IS NOT NULL ) 
            GROUP BY wp_posts.ID 

UNION
        SELECT  SQL_CALC_FOUND_ROWS  wp_posts.*, wp_geodir_gd_attractions_detail.*
        FROM wp_posts 
        INNER JOIN wp_geodir_gd_attractions_detail
        ON (wp_geodir_gd_attractions_detail.post_id = wp_posts.ID) 
        WHERE 1=1 
        AND ( wp_posts.post_status = 'publish' 
        OR wp_posts.post_status = 'private' )
        AND wp_posts.post_type = 'gd_attractions'
        AND wp_geodir_gd_attractions_detail.is_featured = '1'
        AND ( wp_geodir_gd_attractions_detail.geodir_special_offers != ''
        AND wp_geodir_gd_attractions_detail.geodir_special_offers IS NOT NULL ) 
        GROUP BY wp_posts.ID 

ORDER BY ".  $orderby_statement ."
        LIMIT ".$offset.", ".$post_per_page." ";

答案 1 :(得分:0)

不要使用UNION而是编写联接查询来解决您的问题。

答案 2 :(得分:0)

使用此查询并检查其是否有效:-

$sql = "select *
        from wp_posts as 'wpp'
        inner join wp_geodir_gd_arts_detail as 'arts'
        on arts.post_id = wpp.ID
        inner join wp_geodir_gd_hotels_detail as 'gd_hotels'
        on gd_hotels.post_id = wpp.ID
        where
        (wpp.post_status = 'publish' 
        OR wpp.post_status = 'private')
        and (wpp.post_type = 'gd_arts'
        or wpp.post_type = 'gd_hotels')
        and (arts.is_featured = '1'
        or gd_hotels.is_featured = '1')
        and (arts.geodir_special_offers  != ''
        or gd_hotels.geodir_special_offers  != '')
        GROUP BY wpp.ID"

希望这可以满足您的需求,否则,如果遇到任何错误,请告诉我。