初始化然后增加记录集的行号if

时间:2015-02-11 01:09:33

标签: php mysql select

我只是在mysql select query不等于orders_statuspulled时才使用一个pulling来计算和增加一个变量/非现有列,否则就是{ {1}}。我打算在php脚本中使用count来执行empty

标记
row_number=400

初步结果:

$orders_query_raw = "SELECT
                    o.orders_id,
                    o.customers_name,
                    o.payment_method,
                    o.date_purchased,
                    o.delivery_date,
                    o.delivery_time_slotid,
                    o.last_modified,
                    o.currency,
                    o.currency_value,
                    s.orders_status_name,
                    ot.text as order_total
                FROM " . TABLE_ORDERS . " o
                LEFT JOIN " . TABLE_ORDERS_TOTAL . " ot ON (o.orders_id = ot.orders_id), " . TABLE_ORDERS_STATUS . " s
                WHERE o.orders_status = s.orders_status_id
                  AND s.language_id = '" . (int)$languages_id . "'
                  AND s.orders_status_id = '" . (int)$status . "'
                  AND ot.class = 'ot_total'
                ORDER BY o.orders_id DESC";

返回所需结果

orders_id | orders_status 
     34a  |  pulling          
     45a  |  pending      
     45u  |  pulled       
     36c  |  payment_due  

2 个答案:

答案 0 :(得分:1)

您可以根据需要使用变量增加值。您的查询有点复杂,但这是一个想法:

select . . .,
       (case when order_status in ('pulled', 'pulling') then NULL
             else @rn := @rn + 1
        end) as rownumber
from . . . cross join
     (select @rn := 0) vars;

答案 1 :(得分:0)

您可以从此代码开始。只需替换必要的table名称,column名称和variables

$result = mysqli_query($YourDBconnection,"SELECT * FROM yourTable WHERE orders_status<>'pulling' AND orders_status<>'pulled' ORDER BY orders_id");

$count = 1; /* SET A COUNTER */

while($row = mysqli_fetch_array($result)){

  $orderid = $row["orders_id"];
  mysqli_query($YourDBconnection,"UPDATE yourTable SET rownumber='$count' WHERE orders_id='$orderid'"); /* UPDATE THE TABLE */

  $count=$count+1; /* INCREMENT YOUR COUNTER */

}