mysql 偏移量_MySql从查询结果确定行偏移量

bd96500e110b49cbb3cd949968f18be7.png

need to determine the numerical value of the offset of a row by using the value of a column entry in a sorted table.

Given, I know the unique username ($username) in the row of interest.

e.g. $username = "SnowWhite";

Given: it is guaranteed that the $username is in the database and will be in the results of my initial query that follows. Here is my initial query (that works):

$query = "SELECT ALL username,ticket_number, queue_time FROM members WHERE queue_time !=0 ORDER BY queue_time";

$result = mysql_query($query);

What is working is a brute force php search of the $result array for the value of $username.

I would like to replace the php search logic with a query (or a recasting of the above query) that stores the integer value of the row offset within $result for the username column's value of $username into a variable, for example, $offset.

For example, if SnowWhite is in the third row of $result I expect the $offset == 2 (assuming a row index offset starting from 0).

Ultimately (this works) I will update the 'ticket_number' value of the selected row to be == $offset+1. by:

$query="update members set ticket_number='$offset+1' where username='$username'";

mysql_query($query);

Current Working Logic

$query = "SELECT ALL username,ticket_number, queue_time FROM members WHERE queue_time !=0 ORDER BY queue_time";

$result = mysql_query($query);

$i = 0;

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

if ($row[username] == $userinfo) //-- compare usernames

{

++$i;

$query="update members set ticket_number='$i+1' where username='$userinfo'";

mysql_query($query);

break;

}

++$i;

}

mysql_free_result($result);

username ticket_number queue_time

Doc 0 0 The 1st 3 not in sorted result

Grumpy 0 0

Happy 0 0

Sleepy 1 111 The following are ni sorted result

Bashful 2 222

Sneezy 0 333

SnowWhite ??? 444 This is the current user (assign ???=4)

Dopey 0 555

EvilQueen 0 666

解决方案

So with your table example:

username ticket_number queue_time

Doc 0 0 The 1st 3 not in sorted result

Grumpy 0 0

Happy 0 0

Sleepy 1 111 The following are in sorted result

Bashful 2 222

Sneezy 0 333

SnowWhite ??? 444 This is the current user (assign ???=4)

Dopey 0 555

EvilQueen 0 666

how to assign the value 4 to the column ticket_number of the username SnowWhite, 4 being the rank of the row in a sorted selection of the initial rows:

first get the sorted result:

$result = mysql_query("SELECT * FROM table WHERE [here the condition for your sorted array]");

// don't forget to remove the []. they don't go there.

while ($row = mysql_fetch_assoc($result))

{$array[] = $row;}

this should get you something like:

[0] [username] [Sleepy]

[ticket_number] [1]

[queue_time] [111]

[1] [username] [Bashful]

[ticket_number] [2]

[queue_time] [222]

[2] [username] [Sneezy]

[ticket_number] [0]

[queue_time] [333]

[3] [username] [SnowWhite]

[ticket_number] [NULL]

[queue_time] [444]

then foreach through your array

foreach ($array as $number => $row)

{if ($row[username] == $userinfo)

{$result = mysql_query('UPDATE table SET ticket_numer=' . ($number+1) . ' WHERE username=' . $userinfo);}}

the row corresponding to username SnowWhite will get ($number + 1) (3 + 1) as ticket_number

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值