【问题标题】:CF Query of Query multiple order by statements add last ordered column to selectCF Query of Query multiple order by statements add last ordered column to select
【发布时间】:2018-05-28 08:43:39
【问题描述】:

我发现 ColdFusion 的 query 组件有一些非常奇怪的行为。 当您使用此组件构建另一个 ColdFusion Query 的查询 (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");  

Try this on 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

这是 Adob​​e CF 的已知错误吗?

有人知道在 ColdFusion QoQ 中按多列排序的更好方法吗?

【问题讨论】:

  • 记录的排序是否正确?
  • 是的,他们是。您可以在 trycf 要点中看到它,但我也会将结果添加到我的问题中。
  • 当某些 ORDER BY 列也未包含在选择列表中时出现。 Kinda 是有道理的......但如果 Lucee 没有同样的问题,听起来就像一个错误。输出是动态完成的还是可以忽略额外的列?
  • 对我来说看起来像一个错误。这可能没有害处,but still worth reporting
  • 是的,看起来像一个错误。更糟糕的是,交换顺序并查看显示为“col3”的内容。而且我会补充(以防有人想尝试)您的问题也会发生在 queryexecute (而不是“新查询”方法)上。事实上,我的问题也发生在 queryexecute 上: sorted=queryexecute("select [col1],[col2] from unsorted order by [col4], [col3]",[],{dbtype="query"});

标签: coldfusion adobe sql-order-by qoq


【解决方案1】:

好的,我向 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();

当然,您会因此受到很大的性能影响。在 Adob​​e 自行修复错误之前,可能还有其他解决方案。例如,我从 Ben Nadel 那里找到了这个 ancient comment,它展示了在哪里可以找到 cfquery 自定义标签的代码,并认为如果有人愿意的话,可以编辑此代码。

【讨论】:

    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 2022-12-26
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多