有一个php代码,它通过对日期进行排序并选择数据来从数据库中获取数据。通过使用id来尊重并显示结果。
$mysqli=mysqli_connect('localhost','USname','Pass','DB');
//From Ajax
$id = trim($_POST["storedValue"]);
//selact data from database based on id
$query ="SELECT * FROM Data WHERE StudentID='$id' order by Date desc limit 1";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
{
//fetched some id where respect to that data has to be displayed
$Fetch_id =$row[1];
//From Ajax
$id = trim($_POST["storedValue"]);
// Fetching data From database respect to $id "From Ajax " & Fetched data From database i.e. "$Fetch_id"
$query1 ="SELECT * FROM data WHERE StudentID='$id' And Fetch_id='$Fetch_id' ";
$result1 = mysqli_query($mysqli,$query1)or die(mysqli_error());
$num_row1 = mysqli_num_rows($result1);
while($row1=mysqli_fetch_array($result1))
{
// Display final result
echo
"<table >
<tr>
<td>".$row1[6]."<td/>
<td>".$row1[4]."</td>
<td>".$row1[5]."</td>
</tr></table>" ;
}
}
EX:
数据库
slno Fetch_id StudentID date data1 data2 data3
1 AB003 123 2015-10-12 00:00:00 abc3 456 258
2 AB003 123 2015-10-10 00:00:00 abc3 456 258
3 AB003 123 2015-10-09 00:00:00 abc3 456 258
4 AB002 123 2015-10-08 00:00:00 abc2 456 258
5 AB002 123 2015-10-05 00:00:00 abc2 456 258
6 AB002 123 2015-10-04 00:00:00 abc2 456 258
7 AB002 123 2015-10-03 00:00:00 abc2 456 258
8 AB001 123 2015-10-02 00:00:00 abc1 456 258
9 AB001 123 2015-10-01 00:00:00 abc1 456 258
10 AB001 123 2015-09-04 00:00:00 abc1 456 258
11 AB001 123 2015-09-03 00:00:00 abc1 456 258
12 AB001 123 2015-09-02 00:00:00 abc1 456 258
结果:
第一次查询: 仅供参考:StudentID&#34; 123&#34; 在DESC订单 通过将日期和学生ID&#34; 123&#34;
的最新结果排序日期即:
AB003
第二次查询:对于Fetch_id:AB003和StudentID:123
显示结果
data1 data2 data3
abc3 456 258
abc3 456 258
abc3 456 258
期待最终结果应该跳过第一次重复的Fetch_id并选择Next Fetch_id
第一次查询: 仅供参考:StudentID&#34; 123&#34; 显示结果 跳过AB003 ID,应选择AB002
AB002
第二次查询:对于Fetch_id:AB002和StudentID:123
显示结果
data1 data2 data3
abc2 456 258
abc2 456 258
abc2 456 258
abc2 456 258