Coldfusion查找列是否有价值

时间:2018-11-23 11:44:43

标签: sql coldfusion

我有一个表,其中有一个列LanguageID。它具有七个值:1、2、3、4、5、6、22

现在,我有一个函数可以获取当前用户的languageID。

我要检查表中是否存在该languageID,如果存在则返回该值,否则返回默认值2。如果该表中不存在2,则返回1。 < / p>

这是我的查询

<cfquery name="NameName" datasource="mydatabase">
        SELECT DISTINCT SomeID,
        SomeName,
        LanguageID
        FROM myDatabase WITH(NOLOCK)
        WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#" />
        <cfif isDefined(arguments.LanguageID)> //I want to check if the arguments.LanguageID exists in the table
            AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.LanguageID#" />
        <cfelseif NOT isDefined(arguments.LanguageID) > //ako nema value
            AND LanguageID = 2 //default, put languageID to 2
        <cfelse>
            AND LanguageID = 1 //if languageID 2 doesn't exist in that table, then put 1
        </cfif>
    </cfquery>

如此真实的例子。激活该功能,LanguageID为3。没关系,第一次cfif检查表是否具有值3,它具有,确定,然后将languageID设置为3并结束。

第二种情况。该功能激活,LanguageID为44。第一个cfif检查表是否具有值44,它没有该值,因此第二个cfif激活并将LanguageID设置为2

1 个答案:

答案 0 :(得分:3)

这可以在SQL本身中完成。与使用ColdFusion尝试执行查询处理相比,这样做的性能可能更高。

根据您的原始问题,我做出了两个假设。首先,该查询应仅返回1个结果(TOP 1)。其次,您的默认语言是2的{​​{1}}和Default1的{​​{1}}。这还包括您要搜索的值将大于1或2。

1

之所以可行,是因为如果过滤条件不匹配,查询将不返回任何结果,但是当加入具有结果的查询(您的默认设置)时,这些查询将出现在整个查询中。然后,我对结果进行排序并排在前1位。

查询的基本要点位于:https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877

我还提供了一些关于未找到您的Default2时如何工作的演示,以及一种使用默认SELECT TOP 1 SomeID, SomeName, LanguageID FROM ( SELECT SomeID, SomeName, LanguageID FROM t1 WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#"> AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#"> UNION ALL SELECT SomeID, SomeName, LanguageID FROM t1 WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#"> AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */ ) s1 ORDER BY id DESC ; 而非LanguageID或{{1}的默认ID的可能方式}。


并将其全部保存在一个地方:

1
id | lang   
-: | :------
 3 | Klingon
2
id | lang     
-: | :--------
 2 | Default 1
CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
  ,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
  SELECT id, lang 
  FROM t1
  WHERE id = 3

  UNION ALL 

  SELECT id, lang
  FROM t1 
  WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;
id | lang     
-: | :--------
 1 | Default 2
/* No match. */
SELECT TOP 1 id, lang
FROM (
  SELECT id, lang 
  FROM t1
  WHERE id = 99999

  UNION ALL 

  SELECT id, lang
  FROM t1 
  WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;
id | lang
-: | :---
/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
  ,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
  SELECT id, lang 
  FROM t2
  WHERE id = 9999

  UNION ALL 

  SELECT id, lang
  FROM t2 
  WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;
id | lang   
-: | :------
 3 | Klingon
/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
  ,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
  SELECT id, lang 
  FROM t3
  WHERE id = 9999

  UNION ALL 

  SELECT id, lang
  FROM t3 
  WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;
id | lang     
-: | :--------
42 | Default 1

db <>提琴here