如何合并两个查询以显示分页的一组数据

xhv8bpkk  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(375)

现在更新,我想我快到了…:-)
嘿,下面的代码,我发现重复的职位时,滚动通过网页。
我想我的代码的分页部分可能有问题。如果不是的话,它可能是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>
a2mppw5e

a2mppw5e1#

不要使用union,而是编写一个join查询来解决问题。

yzckvree

yzckvree2#

在union/union中,所有查询、排序和限制都必须放在末尾

$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." ";
7hiiyaii

7hiiyaii3#

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

$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"

希望这将工作,你正在寻找,否则让我知道如果你得到任何错误。

相关问题