简体   繁体   中英

How to SELECT specific records having the criteria record id + 1

Currently creating a search page in php for existing system. The data comes from a table with unusual structure of handling of data.

A sample snapshot of existing system, Sales History Webpage

在此处输入图片说明

I observed that if we are going to look the table it is using, the data we saw on Sales History Webpage earlier comes from 1 pair of rows.

在此处输入图片说明

As we can see, if we are going to search for player_id 956 (gang) using SQL query, we would get player_point_id 96834 not 96835. From the existing system, what the Sales History Webpage shows is player_point_id 96835 record but it also shows the recipient player_id 956 (gang). It seems that a pair of record goes like a recipient is found at player_point_id 96834 and the sender is found at player_point_id 96835

Let's say for example the search criteria is player_id 956 only, let' say it found 100 records. It poses a problem that I need to look for the Next records of the player_point_id s it get.

So far I'm trying to do it on php with arrays but it seems a waste I'm running out of ideas. Is this possible to be solved in SQL using an SQL query? If not, other PHP ideas are welcome.

This should work I think. This gets both the rows with player_id 956 and the row(s) with player_point_id one greater than those. You can drop the last line of code if you only want to retrieve the second rows (wasn't really clear from your question what you actually wanted to find).

SELECT t.column, 
       t.another_column 
FROM   table_name t 
WHERE  t.player_point_id IN (SELECT ( player_point_id + 1 ) 
                             FROM   table_name 
                             WHERE  player_id = '956') 
        OR player_id = '956'; 

This might help:

//get current point id

$sql = "select player_point_id from table_name where player_id=$id";
$result=mysqli_query($conn,$sql) or die(mysqli_error($conn));
$player_details = mysqli_fetch_assoc($result);
$ppid = $player_details['player_point_id'];

$ppid += 1;
//get details from the next point id

$sql = "select operator_id,  kiyosuku_id from table_name where player_point_id=$ppid";
$result=mysqli_query($conn,$sql) or die(mysqli_error($conn));
$point_details = mysqli_fetch_assoc($result);
$operator_id = $point_details['operator_id']; //required answer
$kiyosuku_id = $point_details['kiyosuku_id']; //required answer

The code is a little inefficient but would surely work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM