简体   繁体   English

结果集顺序在php中更改

[英]Result set order changing in php

I have a result set and need to be displayed as it is on the screen.But the problem is while displaying each row of the result set using echo command the order is getting changed.Can anyone say why this is happening and provide me a way to overcome it.Here are my actual and printed outputs. 我有一个结果集,需要按原样显示在屏幕上,但是问题是在使用echo命令显示结果集的每一行时,顺序正在更改。任何人都可以说出为什么会发生这种情况并提供一种方法克服它。这是我的实际输出和印刷输出。

Actual Result set: 实际结果集:

JAIKE-ILENE-WACKI-MAZIE-REGLE-SBJ-KMMU  
LVZ-HARTY-MUGZY-STW 
MAZIE-SIXIE-SBJ-KMMU  
PXT-LOUIE-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE  
SWANN-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE  

Output: 输出:

 JAIKE-SBJ-ILENE-KMMU-WACKI-MAZIE-REGLE
 MUGZY-STW-LVZ-HARTY
 SBJ-KMMU-MAZIE-SIXIE
 ILENE-GATBY-WACKI-RAZER-MAZIE-BUZIE-PXT-JAIKE-LOUIE
 WACKI-RAZER-MAZIE-BUZIE-JAIKE-SWANN-ILENE-GATBY  

Here is my code 这是我的代码

$sql3="SELECT GROUP_CONCAT(l.fix_ident SEPARATOR '-') AS fix_seq,l.airport_ident,x.star_ident,x.transition_ident,
                 x.fix_ident from corept.std_star_leg l
                 JOIN
                    (SELECT DISTINCT c.airport_ident,c.star_ident,c.transition_ident,c.fix_ident
                     FROM corept.std_star_leg c
                     INNER JOIN
                          (SELECT star_ident,transition_ident,max(sequence_num) seq,route_type
                           FROM corept.std_star_leg
                           WHERE data_supplier='J'
                           AND airport_ident='KMMU'
                           GROUP BY star_ident,
                           transition_ident)b ON c.sequence_num=b.seq
                           AND c.star_ident=b.star_ident AND c.transition_ident=b.transition_ident
                           LEFT JOIN
                               (SELECT name,trans
                                FROM skyplan_deploy.deploy_stars
                                WHERE apt='KMMU'
                                AND name!=trans) d
                                ON d.name=c.star_ident
                                AND d.trans=c.fix_ident
                                WHERE c.data_supplier='J'
                                AND c.airport_ident='KMMU' AND d.name is null)x
                                where l.airport_ident='KMMU' and l.transition_ident=x.transition_ident
                                and l.star_ident=x.star_ident and l.data_supplier='J'
                                group by x.star_ident,x.transition_ident
                                order by l.star_ident,x.transition_ident,l.sequence_num";
  $res3=mysqli_query($mysqli,$sql3);  
if($res3)
  {
    while($newArray3=mysqli_fetch_array($res3,MYSQLI_ASSOC))
    {
    $apt=$newArray3['airport_ident'];
    $star_ident=$newArray3['star_ident'];
    $trans_ident=$newArray3['transition_ident'];
    $fix_ident=$newArray3['fix_ident'];
    $fix_seq=$newArray3['fix_seq'];
    echo $apt.",".$star_ident.",".$trans_ident.",".$fix_ident.",COREPT,".$fix_seq;
    echo "<br>";
    }
  }
 else
 {
   printf("ERROR:%s\n",mysqli_error($mysqli));
 }

Your query looks overly complex. 您的查询看起来过于复杂。 It appears to be finding the groupwise maximum std_star_leg records by sequence_num (grouped on start_ident and transition_ident ), excluding those for which there is already a matching non-self-referencing deploy_star , then returning the results grouped again with all matching fix_ident values concatenated into a string? 似乎是按sequence_num (在start_identtransition_ident上分组) start_ident按组的最大std_star_leg记录,不包括已经存在匹配的非自引用deploy_star ,然后再次将结果分组,并将所有匹配的fix_ident值串联到串?

If so, the following greatly simplified query ought to achieve the same outcome: 如果是这样,则以下大大简化的查询应该获得相同的结果:

SELECT   GROUP_CONCAT(fix_ident SEPARATOR '-') AS fix_seq,
         airport_ident,
         star_ident,
         transition_ident
FROM     corept.std_star_leg l NATURAL JOIN (
           SELECT   star_ident, transition_ident,
                    data_supplier, airport_ident,
                    MAX(sequence_num) sequence_num
           FROM     corept.std_star_leg
           WHERE    data_supplier = 'J'
                AND airport_ident = 'KMMU'
           GROUP BY star_ident, transition_ident
         ) b
WHERE    NOT EXISTS (
           SELECT NULL
           FROM   skyplan_deploy.deploy_stars d
           WHERE  d.name != d.trans
              AND d.name  = l.star_ident
              AND d.trans = l.fix_ident
              AND d.apt   = l.airport_ident
         )
GROUP BY star_ident, transition_ident

Note that whereas you were previously selecting x.fix_ident in the outermost select list, I have omitted such column because its value would be indeterminately selected by the server from amongst those in the fix_seq . 请注意,虽然您以前在最外面的选择列表中选择x.fix_ident ,但我省略了该列,因为服务器会从fix_seq中的那些值中不确定地选择它的值。

Now, as to your problem (which appears to be related to the order in which fix_ident values appear within the GROUP_CONCAT() string fix_seq —although it's terribly hard to appreciate that from your question), perhaps you want to use the ORDER BY parameter to the GROUP_CONCAT() function? 现在,关于您的问题(这似乎fix_ident值在GROUP_CONCAT()字符串fix_seq显示顺序有关-尽管很难从您的问题中意识到这一点),也许您想使用ORDER BY参数来GROUP_CONCAT()函数? For example: 例如:

SELECT GROUP_CONCAT(fix_ident SEPARATOR '-' ORDER BY ...) AS fix_seq

However, it is not clear to me what ordering you require (the only ORDER BY clause in your original query was entirely redundant). 但是,我不清楚您需要什么排序(原始查询中唯一的ORDER BY子句是完全多余的)。

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

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