如何从函数中获取正确的max id和min id?

时间:2014-10-04 03:57:06

标签: php mysql

我之前有一个300万行的大表,但是我将它分成6个表500.000行,现在我正在尝试连接它们,但是我想读1个表1个而不是所有6个表同时时间,我正在尝试使用我的网站分页来选择需要阅读哪个表以及哪个最大ID和最小ID。

我做了这个功能:

  $content['data']['max_table']  = 6;
  $content['data']['min_table']  = 1;
  $content['data']['limit_page'] = 100;

  function Tables( $start_num, $end_num ) {
   global $content;

   if($end_num <= 500000) {
    $Table_Number = $content['data']['max_table'];
   } else if($end_num > 500000) {
    $Calc         = intval($end_num/500000);
    $Table_Number = $content['data']['max_table']-$Calc;

   }

   $Array                     = array();
   $Array['Table_Number']     = $Table_Number;
   $Array['Max_ID']           = ($Table_Number*500000)-$end_num;
   $Array['Min_ID']           = ($Array['Max_ID']-$content['data']['limit_page'])-$start_num;

   return $Array;
  }

此函数为我提供了正确的Table_Number,但Max_ID和Min_ID是错误的。

页面限制为100,如果我的页码1 = $ start_num为1且$ end_num为100且Table_Number为6 这里Max_ID需要为3000000,Min_ID需要为2999900

如果页码为5000 = $ start_num为499901且$ end_num为500000且Table_Number为6 这里Max_ID需要为2500101,Min_ID需要为2500001

如果页码为5001 = $ start_num为500001且$ end_num为500100且Table_Number为5 这里Max_ID需要为2500000,Min_ID需要为2499900

...

我想通过订单ID描述

来显示
Table 6:
Max id = 3000000
Min id = 2500001

Table 5:
Max id = 2500000
Min id = 2000001

如何从此功能中获取正确的Max_ID和Min_ID,以便我选择正确的方法?

2 个答案:

答案 0 :(得分:1)

试试这个:

$Array['Min_ID'] = 6 * 500000 - $end_num + 1;    
$Array['Max_ID'] = $Array['Min_ID'] + 99;

答案 1 :(得分:0)

我成功修复了这个功能:

  function Tables( $start_num, $end_num ) {
   global $content;

   if($end_num <= 500000) {
    $Table_Number = $content['data']['max_table'];
   } else if($end_num > 500000) {
    $Calc         = intval($end_num/500000);
    $Table_Number = $content['data']['max_table']-$Calc;
   }

   $Table_Extra                = $content['data']['max_table']-$Table_Number;

   if($Table_Extra == 0) {
    $Max_Table_ID              = $Table_Number*500000;
    $Min_ID                    = ($Max_Table_ID-$end_num)+1;
    $Max_ID                    = $Min_ID+99;
   } else if($Table_Extra > 0) {
    $Add_Extra                 = $Table_Extra*500000;
    $Max_Table_ID              = $Table_Number*500000;
    $Min_ID                    = (($Max_Table_ID-$end_num)+1)+$Add_Extra;
    $Max_ID                    = ($Min_ID+99)+$Add_Extra;
   }  

   $Array                     = array();
   $Array['Table_Number']     = $Table_Number;
   $Array['Max_ID']           = $Max_ID;
   $Array['Min_ID']           = $Min_ID;


   return $Array;
  }