[英]SQL Union with coalesce(?) in where clause
我正在尝试将旧的帮助台记录合并到更新的帮助台应用程序中。 我仍然拥有旧帮助台应用程序中的所有数据,它使用与新应用程序相同的字段,但位于不同的文件中。 对于较新的应用程序来说,此select语句工作得很好,它可以搜索所有过去的调用以查找特定的内容,无论它是关键字还是分配给谁等等。
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
我想做的是使用某种联合,所以我只能用一个框来搜索新记录和旧记录,而不是两个不同的框,并且必须记住数据的存储位置。 我以为这样的事情可能会起作用,但是我认为我的where子句太含糊了,我几乎尝试了在字段前面包括一个库的所有组合。
Select *
From
(
select status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table1
Union
select status, identity, description, contact, scan_text, extended_desc, allocated_to
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
如果仅对两个选择进行联合,那么我将从两个表中获取所有记录,但是我需要能够将结果范围缩小到关键字或某种其他类型的字段,例如在第一段代码中。 如果有人能指出正确的方向,我将不胜感激。
我可能还应该说这是db2,而sql正在Web应用程序上运行。 因此,当此sql运行时,它会生成下拉框或文本字段,以将您自己的文字放入其中以缩小所有帮助台调用的结果。
如果我理解正确,则需要指示记录来自哪个表的指示器。 如果是这样的话:
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
列(并重新命名为任何你想和列)。
您总是可以只在要合并的两个select语句中放置where。 可能比合并两个表然后进行筛选要快。
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))
语法可能会更好。
我不知道这是否特定于我正在使用的Web应用程序生成器,或者是否可以在其他SQL中将其用于其他地方,但是您可以引用参数,并通过称为parameter parameter的参数将它们传递给幕后。联合的后半部分中的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
谢谢那些试图帮助我的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.