2

I need to count the number of results for pagination.

Demo Query

select A.order_id, 
       IF(
          E.assign_date IS NOT NULL AND E.assign_date != '0000-00-00',  
          DATE_FORMAT(E.assign_date , '%d-%b-%y'),
          (select data from other tables with other conditions)
       ) AS assign_date, 
       .......
       where 
       .....
       and assign_date > 2013-08-01

Count Query which I am using

Select count(A.order_id) from order A, size B, production E ........... where ....... and assign_date > 2013-08-01

I need to add that if condition so I can count the results properly

Longer Version

I have checked the questions posted on SO, and couldnt find the answer. I am working on a page where I am showing pagination.

For pagination I have used the count, which is working. Count query is not having this condition.

For actual query I wish I could post my whole query over here but it is over 200+ lines if sorted well.

For pagination I am using the following query.

$strQuery = "SELECT COUNT(DISTINCT(A.order_id)) AS totalRecs FROM $tbls WHERE $whereClause $WhereExt";

The $tbls and $whereClause are very long.

In orders table I m having assing_on date field, which shows that an order is assigned to some one for production. I have created the result list page from the data, and in PHP added check that if assign_on date is empty/null. If it is I am running another query to get estimated start date of production.

The estimated start date of production gets a date on number of calculations and parameters

  1. Orders.estimated delivery date (this is set when posting the order)
  2. mustReadyDate = estimated date - shipment buffer (another table) - vendor holidays (another 3 tables) - vendor weekends (another 2 tables)
  3. totalDaysRequired for production = total length of order / Standard weave rate per day (another table)
  4. muststartdate = mustReadyDate - totalDaysRequired;

Above four points are all in query, no PHP work involve

Data fetch query is having this condition and count query is not having this condition at the moment.

IF(
    E.assign_date IS NOT NULL AND E.assign_date != '$mysqlEmptyDate',  
    DATE_FORMAT(E.assign_date , '" . reporting_DateFormat . "'),
    ($mustLoomStartDateQry_fm)
) AS assign_date, 

I need to add one more condition in where clause of count query to check if aasign_date > one week from today I just want to know how I can add such a long query in count part so I only execute on query.

** EDIT **

If any one is interested in viewing the full query http://pastebin.com/zqzbpEei but please I am providing you this only to give you the insight...

4

1 回答 1

0

如果您需要分页,请尝试这个.......

$Query="select * from table_name_here where colunm_name='content_which_you_find'";

例如

$Query="select * from android where type='Casual'";

如果您不需要条件,请删除它......

喜欢$Query="select * from android";

现在使用这个执行上面的查询

$Result=mysql_query($Query);

现在获取使用此选择的总行数

$TotalRows=mysql_num_rows($Result);

$onepage=9;

将 9 以上更改为您希望在一页中显示多少记录

现在粘贴以下代码以获取分页

$d=($page-1)*$onepage;
$SqlQuery="select * from android where type='Casual' order by id desc LIMIT $d, $onepage";
$ResultSql=mysql_query($SqlQuery) or die("error1?");

现在放循环打印数据

while($row = mysql_fetch_array($ResultSql))
{
echo $row[1];
echo $row[2];
}

现在把下面的代码

 for($i=1;$i<=$np;$i++)
{
echo "<a href='currnt_page_name_here.php?page=$i'> $i </a>";
}

如果有任何错误告诉我,请立即尝试

于 2013-06-13T11:15:41.500 回答