Tuesday, 15 January 2013

sql - Check order of rows in mysql -



sql - Check order of rows in mysql -

need help mysql.

i have table total of bids, need check if there more 3 bids from same user 1 after another.

here code example:

$all = sql_get('select bids bids auction_id = 1 order amount asc'); $total = 0; foreach ($all $bid) { if ($bid->user_id == $user->id) { if (++$total <= 3) continue; $bid->burned = true; sql_store($bid); show_error('you cant have more 4 bids 1 after another, lastly bid burned.'); } else { $total = 0; } }

is there way in single query ?

select user_id, case when @user_id = user_id @rownum := @rownum + 1 else ((@user_id := user_id) * 0) + (@rownum := 1) end rownum ( select user_id bids, (select @rownum := 0, @user_id := null) vars auction_id = 1 order created desc) h having user_id = '{$user->id}' , rownum >= 3

simply parse user_id query , know if there 3 in row. asumes save time rows created created column.

mysql sql

No comments:

Post a Comment