【问题标题】:Query was empty error while selecting and updating the record?选择和更新记录时查询为空错误?
【发布时间】:2014-02-07 06:37:22
【问题描述】:

我想知道为什么我在检查 Sqlyog 中运行良好的查询时收到此错误 "Query is Empty"。任何人都可以通过解释此查询中的问题来帮助我吗?任何帮助,将不胜感激。

这是我的代码。我在 Ubuntu 上使用 Railo 4.2.0。

<cfquery name="q" datasource="#getDSN()#">
    SELECT is_nav_item, nav_order, MAX(nav_order) AS maxNavOrder 
    FROM content
    WHERE id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.id#">
    OR site__id = <cfqueryparam value="#arguments.site__id#" />
</cfquery>

<cfquery datasource="#getDSN()#">
    <!--- 
       check to see if existing record has 0 and in arguments it is 
       passed as 1 then we are going to update the nav order by selecting 
       max nav order in the table then incrementing the maxNavOrder by 1
    --->
    <cfif q.is_nav_item EQ 0 AND arguments.is_nav_item EQ 1>
        UPDATE content
        SET nav_order = #q.maxNavOrder+1#
        WHERE id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_integer" />
        AND site__id = <cfqueryparam value="#arguments.site__id#" />
    </cfif>

    <!--- 
        check to see if the existing record have is_nav_item eq 1 and in 
        the arguments it is 0 if it is then its going to update the nav_order 
        to 0... so that we can prevent it being updating the nav order in case 
        someone not updating nav_item but something else
    --->
    <cfif q.is_nav_item EQ 1 AND arguments.is_nav_item EQ 0>
        UPDATE content
        SET nav_order = 0
        WHERE id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_integer" />
        AND site__id = <cfqueryparam value="#arguments.site__id#" />
        ;
    </cfif>
 </cfquery>

【问题讨论】:

    标签: mysql coldfusion coldfusion-9 railo


    【解决方案1】:

    您的查询中有 if 语句...如果这两个语句的计算结果均不正确,则您的查询可能是一个空查询。

    为什么不做一个 IF 并将整个查询放在 IF 语句中……这样就没有办法在没有任何文本的情况下获得 CFQUERY。

    <!--- 
      check to see if existing record has 0 and in arguments it is passed as 1 
      then we are going to update the nav order by selecting max nav order in 
      the table then incrementing the maxNavOrder by 1
    --->
    <cfif q.is_nav_item EQ 0 AND arguments.is_nav_item EQ 1>
         <cfquery datasource="#getDSN()#">
            UPDATE content
            SET    nav_order = #q.maxNavOrder+1#
            WHERE  id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_integer" />
            AND site__id = <cfqueryparam value="#arguments.site__id#" />
         </cfquery>
    </cfif>
    
    <!--- 
        check to see if the existing record have is_nav_item eq 1 and in the 
        arguments  it is 0.  if it is then its going to update the nav_order 
        to 0... so that we can prevent it being updating the nav order in case 
        someone not updating nav_item but something else
    --->
    <cfif q.is_nav_item EQ 1 AND arguments.is_nav_item EQ 0>
        <cfquery datasource="#getDSN()#">
           UPDATE content
           SET    nav_order = 0
           WHERE  id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_integer" />
           AND    site__id = <cfqueryparam value="#arguments.site__id#" />
        </cfquery>
    </cfif>
    

    【讨论】:

    • 其实我听说使用 cfquery 的次数会导致性能问题,因为每当执行 cfquery 标签时,它都会调用数据库。
    • 如果这样做,第一个更新不工作第二个工作。
    • 如果您使用 1000 个查询,是的,您会想以某种方式对它们进行分组,但对于基础知识,像这样分解它们是完全可以的。它也使阅读变得更容易。如果第二个运行,它会覆盖你的第一个......所以两者都可以运行,但它们是相互对立的。
    • (编辑)@GavinPickin - 仅供参考,您可以使用代码按钮,即{}(或缩进 4 个以上空格)发布 cfml。
    • 感谢 Leigh,刚接触这个...但只是想尽我所能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多