简体   繁体   English

重新排序Mysql查询结果

[英]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. 我有以下mysql查询,其中Id是从1,2 ...到450等自动递增的主键。

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

and the following PHP snippet: 以及以下PHP代码段:

<?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 ) : 当我使用当前查询和php代码时,我得到结果( IdData 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 ASC order ,数据顺序不会改变):

    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 ). 如果我理解正确,您不希望看到针对行的实际ID,而只是从1到200的数字序列号(您有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']}]";
}

You can use a simple inline variable within the sql ( similar to that shown above by @Raymond ) 你可以在sql中使用一个简单的内联变量(类似于@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. 这种方法需要注意的一点是@var的值在查询结束时保留,因此后续查询将继续增加该值。 To negate that you would set the value to null after running the initial query - ie set @var=null; 要否定在运行初始查询后将值设置为null - 即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. 如果查询的最终用法是HTML中的简单显示,则css变量可能是最简单的选项,并且不需要基本sql查询的任何其他mod。

You can get id data and other data separately And then display them at the same time 您可以分别获取id数据和其他数据然后同时显示它们

$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部分:

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

You can use MySQL's user variables to simulate ROW_NUMBER() under MySQL versions under MySQL 8.0. 您可以使用MySQL的用户变量来模拟MySQL 8.0下的MySQL版本下的ROW_NUMBER()
you need atleast MySQL 5.0.51b to be able to use MySQL's user variables. 你需要至少MySQL 5.0.51b才能使用MySQL的用户变量。

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

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 希望能帮助到你

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

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