Reordering Mysql query results

  • A+
Category:Languages

I have the following mysql query where Id is the primary key auto incremented starting from 1,2...to 450 etc.

$query = "SELECT Id, PromA, PromB, PromC FROM Promtab Order by Id DESC Limit 200";  $aresult = $con->query($query); 

and the following PHP snippet:

 <?php  while($row = mysqli_fetch_assoc($aresult)){ echo "[".$row["Id"].", ".$row["PromA"].",".$row["PromB"].",".$row["PromC"]."],"; }  ?>        

When I use the current query and php code I get the results: (Id and Data DESC order)

    Id,  PromA, PromB, PromC     450   230    220    200     449   150    140    180     448   221    70     145     .     .     1     120    110    105 

What I need is this: (ONLY Id ASC order, Data order doesn't change)

    Id,  PromA, PromB, PromC      1   230    220    200      2   150    140    180      3   221    70     145      .      .     450    120    110    105 

How can I do this simply? thanks.

 


If I understand you correctly, you don't want to see the actual IDs against rows, but simply the numerical sequential numbers from 1 to 200 (you have LIMIT 200). In this case what you can do is something like this:

$query = "SELECT PromA, PromB, PromC FROM Promtab Order by Id DESC Limit 200";  $aresult = $con->query($query);  $id = 1; while ($row = mysqli_fetch_assoc($aresult)) {     echo "[" . ($id++) . ", {$row['PromA']}, {$row['PromB']}, {$row['PromC']}]"; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: