[英]get a particular column element of the next table row from database
I have a query which selects userid,messageid,statusid
from tableA
like following. 我有一个查询
userid,messageid,statusid
从tableA
选择userid,messageid,statusid
,如下所示。
$qry = mssql_query('select userid,messageid,statusid from tableA');
if(mssql_num_rows($qry))
{
$data = mssql_fetch_array($qry)
{
//if(current_status_id column value != next_status_id column value)
$status = $data['statusid'];
}
}
I need to compare the value of current statusid
column with the immediate next row statusid
column like this if(current_status_id column value != next_status_id column value)
.Is this possible.Pls help me 我需要将当前
statusid
列的值与紧邻的下一行statusid
列进行比较,例如if(current_status_id column value != next_status_id column value)
。是否可以。请帮我
$qry = mssql_query('select userid,messageid,statusid from tableA order by statusid');
while (($row=mssql_fetch_array($qry) !== FALSE) {
if (isset($previous) && $previous['statusid'] !== $row['statusid']) {
// do what you gotta do here
}
$previous = $row;
}
I added order by statusid
to your SQL so that you do get an order set of data. 我向您的SQL添加了
order by statusid
以便您获取订单数据集。 And rather than trying to "look ahead" to the next row, the code above "looks back" to the previous row ... which is effectively the same. 而不是试图“向前看”下一行,而是上面的代码“向后看”到上一行...实际上是相同的。 You've got the data of the two rows in
$previous
and $row
so you should be able to do what you wanna do with $previous
. 您已经在
$previous
和$row
获得了两行的数据,因此您应该能够使用$previous
。
I have created an array and assigned the results into it.Then I used while loop and checked if the current element is not equal to next element. 我创建了一个数组并将结果分配给它。然后使用while循环检查当前元素是否不等于下一个元素。
$i=1;
while (($row = mssql_fetch_array($qry, MYSQL_ASSOC)) !== false)
{
$data_array[] = $row; // add the row in to the results (data) array
}
mssql_data_seek( $qry, 0 );
while($msgDetailChilds = mssql_fetch_array($qry)){
if($data_array[$i]['groupid']!=$data_array[$i-1]['groupid'])
{
//do stuff
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.