简体   繁体   English

查询返回 3 行的相同数据

[英]Query returns the same data for 3 rows

I am getting the same value for the 1st, 2nd and 3rd.我在第 1、第 2 和第 3 次获得相同的值。 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;快捷方式有 a, b, c 并且它们的等级 a =1 b =2 c =3;

Then 1st = a, 2nd = b, 3rd = c;然后第 1 个 = a,第 2 个 = b,第 3 个 = c;

What im getting is我得到的是

1st = a, 2nd = a, 3rd = a;第一个 = 一个,第二个 = 一个,第三个 = 一个;

then the next line shows然后下一行显示

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

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

It solves as follows:它解决如下:

  • If rank equals 1 then rank = 1 is 1 else 0如果rank等于 1,则rank = 1为 1 否则为 0
  • Can either 0 or 1 be greater than 2? 0 或 1 可以大于 2 吗? No.不。
  • So, rank = 1 > 2 resolves always to false and output of IF will be thirst parameter ie shortcut.因此, rank = 1 > 2始终解析为 false,并且IF输出将是口渴参数,即快捷方式。

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.如果 rank 为 3,则rank = 3为 1,否则为 0。
  • Can rank = 3 (output 0 or 1) be equal to 3? rank = 3 (输出 0 或 1)可以等于 3 吗? No.不。
  • So, rank = 3 = 3 always return false, and thus output is always the 3rd parameter ie shortcut.所以, rank = 3 = 3总是返回false,因此输出总是第三个参数,即快捷方式。

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.关于您的预期输出,请编辑您的问题以添加示例数据和基于它的预期输出以及逻辑的完整解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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