Jason
Jason

Reputation: 163

Reordering Mysql query results

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.

Upvotes: 2

Views: 454

Answers (5)

Raymond Nijland
Raymond Nijland

Reputation: 11602

You can use MySQL's user variables to simulateROW_NUMBER() under MySQL versions under MySQL 8.0.
you need atleast MySQL 5.0.51b to be able to use MySQL's user variables.

SELECT 
   (@ROW_NUMBER := @ROW_NUMBER + 1) AS Id
 , Promtab_ordered.* 
FROM (
  SELECT
      Promtab.PromA
    , Promtab.PromB
    , Promtab.PromC
   FROM
     Promtab
   ORDER BY
    Promtab.Id DESC
   LIMIT 200
) AS Promtab_ordered
CROSS JOIN ( SELECT @ROW_NUMBER := 0 ) AS init_user_param
ORDER BY 
 Id ASC

Upvotes: 2

Professor Abronsius
Professor Abronsius

Reputation: 33823

You can use a simple inline variable within the sql ( similar to that shown above by @Raymond )

select ( @var := ifnull( @var, 0 ) + 1 ) as `row`, `id`, `proma`,`promb`,`promc` 
from `promtab` 
order by `id` desc;

One thing to note with this approach is the value of @var is maintained at the end of the query so subsequent queries would continue to increment the value. To negate that you would set the value to null after running the initial query - ie set @var=null;

If the end usage of the query is a simple display in HTML a css variable might be the simplest option and would not require any additional mods to the base sql query.

Upvotes: 3

wadge
wadge

Reputation: 428

If I understood correctly, you are effectively seperating the order displayed from the order that comes from the database.

If that's the case, and since that you are returning the results as an array, you can iterate the results something in the lines of (not tested):

$resultsArray = mysqli_fetch_array($aresult));
for($i = 0; $i <= count($resultsArray); $i){
    // $i will be your index. Your query will return the results descending
}

hope it helps

Upvotes: 2

Dipanshu Mahla
Dipanshu Mahla

Reputation: 152

You can get id data and other data separately And then display them at the same time

$queryForId = "SELECT Id FROM Promtab Order by Id ASC Limit 200"; 
$queryForData = "SELECT PromA, PromB, PromC FROM Promtab Order by Id DESC Limit 200";
$resultForId = $con->query($queryForId);
$resultForData = $con->query($queryForData);

and the php part as:

<?php
while($rowId = mysqli_fetch_assoc($resultForId)){
  $rowData = mysqli_fetch_assoc($resultForData);
echo "[".$rowForId["Id"].", ".$rowForData["PromA"].",".$rowForData["PromB"].",".$rowForData["PromC"]."],";
}
?>   

Upvotes: 2

Aleks G
Aleks G

Reputation: 57346

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']}]";
}

Upvotes: 8

Related Questions