简体   繁体   中英

Query returns the same data for 3 rows

I am getting the same value for the 1st, 2nd and 3rd. Then the next line gives me the results supposed to be in the 2nd column supposed in the first row.

<?php  
 require "db.php"; 
 $sql = "select n_name, shortcut, IF(rank = 1>2, 2, shortcut) AS 1st, IF(rank = 1<2, shortcut, shortcut) AS 2nd, 
 IF(rank = 3=3, 1, shortcut) as 3rd from team inner join nonsport on team.n_id = nonsport.n_id";                         

 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); 

$result = mysqli_query($con,$sql);


 $response = array();

while($row=mysqli_fetch_array($result))
{
array_push($response, array("n_name"=>$row[0],"1st"=>$row[1], 
"2nd"=>$row[2], "3rd"=>$row[3]));

} 
echo json_encode (array("nresults"=>$response));



mysqli_close($con);

?>

My expected output is

Example. Shortcut has a, b, c and they have rank a =1 b =2 c =3;

Then 1st = a, 2nd = b, 3rd = c;

What im getting is

1st = a, 2nd = a, 3rd = a;

then the next line shows

1st = b, 2nd = b, 3rd = b;

IF(rank = 1 > 2, 2, shortcut)

It solves as follows:

  • If rank equals 1 then rank = 1 is 1 else 0
  • Can either 0 or 1 be greater than 2? No.
  • So, rank = 1 > 2 resolves always to false and output of IF will be thirst parameter ie shortcut.

IF(rank = 1 < 2, shortcut, shortcut)

This one returns shortcut always as 2nd and 3rd param both are shortcut.


IF(rank = 3 = 3, 1, shortcut)

In this,

  • If rank is 3 then rank = 3 is 1 else 0.
  • Can rank = 3 (output 0 or 1) be equal to 3? No.
  • So, rank = 3 = 3 always return false, and thus output is always the 3rd parameter ie shortcut.

Hence why all the three functions return the same value.

Regarding your expected output, please edit your question to add sample data and expected output based on it along with complete explanation of the logic.

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