SQL联合在where子句中使用coalesce(?)

时间:2016-02-16 21:32:27

标签: sql web-applications db2 union

我正在尝试将旧的帮助台记录合并到更新的帮助台应用程序中。我仍然拥有旧帮助台应用程序中的所有数据,它使用的字段与新字段相同,但它位于不同的文件中。这个select语句适用于较新的应用程序,可以搜索所有过去调用的特定内容,无论是关键字还是分配给谁,等等。

public static void BeepOnKey()
{
    int frequency = 10000;
    int duration = 100;

    Console.WriteLine("Use keyboard arrows to adjust frequency and duration");
    do
    {
        while (!Console.KeyAvailable)
        {
            Console.Beep(frequency, duration);
            //this method emits beep sound from system speakers
        }

        var mykey = Console.ReadKey(true); //Read The Key

        switch (mykey.Key)
        {
            case ConsoleKey.UpArrow:
                Console.WriteLine("UpArrow Pressed");
                frequency += 100;
                frequency = Math.Min(frequency, 15000);
                //do your stuff
                break;
            case ConsoleKey.DownArrow:
                Console.WriteLine("DownArrow Pressed");
                frequency -= 100;
                frequency = Math.Max(frequency, 1000);
                //do your stuff
                break;
            case ConsoleKey.RightArrow:
                Console.WriteLine("RightArrow Pressed");
                duration += 100;
                duration = Math.Min(duration, 1000);
                //do your stuff
                break;
            case ConsoleKey.LeftArrow:
                Console.WriteLine("LeftArrow Pressed");
                duration -= 100;
                duration = Math.Max(duration, 100);
                //do your stuff
                break;
        }
    } while (true);
}

我想要做的是使用某种联合,这样我就可以只有一个盒子来搜索新旧记录,而不是两个不同的盒子,并且必须记住数据的存储位置。我认为这样的事情可能有用,但我认为我的where子句太模糊了,我尝试了在字段前面包含一个库的每个组合。

SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
 FROM   helpdesk.table1
 WHERE  UPPER(allocated_to) = coalesce(?, allocated_to) 
  AND   identity = coalesce(?, identity) 
  AND   description = coalesce(?, description) 
  AND   contact= coalesce(?, contact) 
  AND UPPER(scan_text) LIKE coalesce(?,scantext)
  and upper(extended_desc) like coalesce(?, extended_desc)
 ORDER by allocated, identity desc

如果我只使用两个选项进行联合,我会从两个表中获取所有记录,但我需要能够将结果缩小到关键字或其他类型的字段,就像在第一个代码块中一样。如果有人能指出我正确的方向,我会非常感激。

另外我可能应该说它是db2并且sql在web应用程序上运行。因此,当这个sql运行时,它会生成下拉框或文本字段以放入您自己的单词以缩小所有帮助台呼叫的结果。

3 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要指示记录来自哪个表。如果是这样的话:

Select *
From (select 'old' as which, status, identity, description, contact, scan_text, extended_desc, allocated_to
      from helpdesk.table1
      Union all                                
      select 'new', status, identity, description, contact, scan_text, extended_desc, allocated_to
      from helpdesk.table2
     ) hd
WHERE UPPER(allocated_to) = coalesce(?, allocated_to) AND
      identity = coalesce(?, identity) AND
      description = coalesce(?, description) AND
      contact= coalesce(?, contact) AND
     UPPER(scan_text) LIKE coalesce(?,scantext) AND
     upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc;

然后,您可以在where列上添加相应的which逻辑(并将列重命名为您想要的任何内容)。

答案 1 :(得分:0)

您可以随时将两个选择语句中的位置放在您尝试合并的位置。可能比组合两个表更快,然后过滤。

SELECT * FROM  helpdesk.table1
WHERE   UPPER(allocated_to) = coalesce(?, allocated_to) 
  AND   identity = coalesce(?, identity) 
  AND   description = coalesce(?, description) 
  AND   contact= coalesce(?, contact) 
  AND UPPER(scan_text) LIKE coalesce(?,scantext)
  and upper(extended_desc) like coalesce(?, extended_desc)
UNION ALL 
SELECT * FROM  helpdesk.table2
WHERE   UPPER(allocated_to) = coalesce(?, allocated_to) 
  AND   identity = coalesce(?, identity) 
  AND   description = coalesce(?, description) 
  AND   contact= coalesce(?, contact) 
  AND UPPER(scan_text) LIKE coalesce(?,scantext)
  and upper(extended_desc) like coalesce(?, extended_desc)
ORDER BY allocated, identity desc;

在第二次选择后按顺序排序。如果您担心案例,UPPER(allocated_to) = coalesce(?, UPPER(allocated_to))语法可能会更好。

答案 2 :(得分:0)

我不知道这是否特定于我正在使用的Web应用程序生成器,或者您是否可以在不同的SQL中使用它的其他位置,但您可以引用这些参数并在幕后传递它们一个参数参数,它们是联合后半部分的?? 1,?? 2,?? 3。这解决了通过传递你在第一个输入的内容而有两组用于用户输入的框的问题。这是适用于我的代码的最终版本

SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM   helpdesk.table1
WHERE  UPPER(allocated_to) = coalesce(?, allocated_to) 
 AND   identity = coalesce(?, identity) 
 AND   description = coalesce(?, description) 
 AND   contact= coalesce(?, contact) 
 AND UPPER(scan_text) LIKE coalesce(?,scantext)
 and upper(extended_desc) like coalesce(?, extended_desc)

Union

SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM   helpdesk.table2
WHERE  UPPER(allocated_to) = coalesce(??1, allocated_to) 
 AND   identity = coalesce(??2, identity) 
 AND   description = coalesce(??3, description) 
 AND   contact= coalesce(??4, contact) 
 AND UPPER(scan_text) LIKE coalesce(??5,scantext)
 and upper(extended_desc) like coalesce(??6, extended_desc)
ORDER by allocated, identity desc

感谢那些试图帮助我的人。

相关问题