查询多个订单的CF查询按语句添加最后排序的列进行选择

San*_*der 5 coldfusion adobe sql-order-by qoq

我发现ColdFusion的查询组件有一些非常奇怪的行为.当您使用此组件构建另一个ColdFusion查询的查询(QoQ)查询并order by在多个列上使用时,order by列表中的最后一列将添加到所选输出中.这似乎发生在CF9,10,11和2016,但不是Lucee.

/* Create an unsorted CF-query */
unsorted = QueryNew("col1,col2,col3,col4","VarChar,VarChar,Integer,VarChar");
for (a=10;a gte 1;a--){
    QueryAddRow(unsorted);
    QuerySetCell(unsorted,"col1","col1 #a#");
    QuerySetCell(unsorted,"col2","col2 #a#");
    QuerySetCell(unsorted,"col3","#a#");
    QuerySetCell(unsorted,"col4","col4 #a#");
}

writeDump(var="#unsorted#");

/* Create a new CF query of query with the unsorted table */ 
sorted = new query(
    dbtype = "query"
    ,unsorted = unsorted
    ,sql = "select [col1],[col2] from unsorted order by [col3], [col4] asc"
    ).execute().getresult();

/* The last column in the order by list will be displayed in the result */  
writeDump(var="#sorted#", label="sorted");  
Run Code Online (Sandbox Code Playgroud)

在trycf.com上试试 这是最后一个查询的结果:

    col1        col2        col4
1   col1 1      col2 1      1
2   col1 2      col2 2      2
3   col1 3      col2 3      3
4   col1 4      col2 4      4
5   col1 5      col2 5      5
6   col1 6      col2 6      6
7   col1 7      col2 7      7
8   col1 8      col2 8      8
9   col1 9      col2 9      9
10  col1 10     col2 10     10
Run Code Online (Sandbox Code Playgroud)

这是Adobe CF的已知错误吗?

有没有人知道ColdFusion QoQ中多列的不同,更好的订购方式?

San*_*der 0

好吧,我向 Adob​​e 报告了该错误: https: //tracker.adobe.com/#/view/CF-4200408并决定通过使用两个 QoQ 来解决该错误,分离数据的排序和列的选择,像这样:

/* Create an unsorted CF-query */
unsorted = QueryNew("col1,col2,col3,col4","VarChar,VarChar,Integer,VarChar");
for (a=10;a gte 1;a--){
    QueryAddRow(unsorted);
    QuerySetCell(unsorted,"col1","col1 #a#");
    QuerySetCell(unsorted,"col2","col2 #a#");
    QuerySetCell(unsorted,"col3","#a#");
    QuerySetCell(unsorted,"col4","col4 #a#");
}

writeDump(var="#unsorted#");

/* Create a new CF query of query with the unsorted table */ 
sorted = new query(
    dbtype = "query"
    ,unsorted = unsorted
    ,sql = "select [col1],[col2] from unsorted order by [col3], [col4] asc"
    ).execute().getresult();

/* The last column in the order by list will be displayed in the result */  
writeDump(var="#sorted#", label="sorted");  

selected = new query(
    dbtype = "query"
    ,sorted = sorted
    ,sql = "select [col1],[col2] from sorted"
    ).execute().getresult();
Run Code Online (Sandbox Code Playgroud)

当然,你会因此受到很大的性能影响。在 Adob​​e 自行修复该错误之前,可能还有其他解决方案。例如,我发现 Ben Nadel 的这条古老评论演示了在哪里可以找到 cfquery 自定义标记的代码,并认为如果有人愿意的话,可以编辑该代码。