我在解决此查询时遇到问题。在我的where子句中,我有一个逻辑,它只给我表中的活动字段。但我想要检查我的复选框是否给我活动和非活动元素。我怎么解决这个问题?是否有可能将cfif放在where子句或其他方式?这是我的疑问:
string noseperator = this.txtdate1.Text.Replace("\\", "");
这是我的复选框代码:
SELECT Date, Name, Time, Active
Case when Active = 1 then 'Yes' else 'No' end as Active
FROM City
<cfif form.ActiveCheck GT 0>
WHERE Active = 0
</cfif>
ORDER BY Date DESC
答案 0 :(得分:2)
如果我理解正确,当勾选复选框时,您不希望按查询中的活动列进行过滤?要做到这一点,你会做类似的事情:
<cfquery name="myQ">
SELECT Date, Name, Time, Active
Case when Active = 1 then 'Yes' else 'No' end as Active
FROM City
<cfif !structKeyExists(form, "ActiveCheck")>
<!--- checkbox is not ticked so just get inactive --->
WHERE Active = <cfqueryparam value="0" cfsqltype="cf_sql_integer">
</cfif>
ORDER BY Date DESC
</cfquery>
....
<input type="Checkbox" name="ActiveCheck" id="ActiveCheck" value="0">All<br>
答案 1 :(得分:0)
这类似于约翰的回答,但我不喜欢<cfif>
内的<cfquery>
。我希望数据库服务器拥有尽可能少的查询计划。 注意: IIF()是一种老派的方式。
<cfquery name="myQ">
DECLARE @Active bit = <cfqueryparam value="#IIF(stuctkeyExists(form, 'activecheck'), 1, 0)#" cfsqltype="cf_sql_bit">
SELECT Date, Name, Time, Active,
Case when Active = 1 then 'Yes' else 'No' end as Active
FROM City
WHERE @ActiveCheck = 0
OR (@ActiveCheck = 1 AND Active = 1)
ORDER BY Date DESC
</cfquery>