0

我有一个搜索字段,在这种情况下用户搜索“RG Transport”。我首先想要最精确的匹配。

这是生成的查询:

SELECT DISTINCT department.bold_id, 
                departmentid + ' ' + organization.name + ' - ' 
                + department.name AS DisplayValue, 
                organization.name 
FROM   organization, 
       department 
WHERE  organization.bold_id = department.organization 
       AND Upper(searchkeysname) LIKE '%[[]%RG%]%' 
       AND Upper(searchkeysname) LIKE '%[[]%TRANSPORT%]%' 
ORDER  BY organization.name 

“RG Transport AB”在第 78 行。所以我希望它在第一行。然后我尝试与工会

SELECT DISTINCT department.bold_id, departmentid + ' ' + organization.name + ' - ' + department.name AS DisplayValue, 
organization.name  FROM Department, Organization
WHERE organization.bold_id = department.organization 
AND Upper(searchkeysname) LIKE '%[[]RG TRANSPORT%]%' 
UNION ALL
SELECT DISTINCT department.bold_id, 
                departmentid + ' ' + organization.name + ' - ' 
                + department.name AS DisplayValue, 
                organization.name 
FROM   organization, 
       department 
WHERE  organization.bold_id = department.organization 
       AND Upper(searchkeysname) LIKE '%[[]%RG%]%' 
       AND Upper(searchkeysname) LIKE '%[[]%TRANSPORT%]%' 

它工作正常,但随后的行没有按名称排序。如果我添加

按组织名称订购

到最后一个查询,第一行丢失了。我还有其他办法吗?

4

1 回答 1

0

只需在联合的上半部分添加一个 1 的列,在下半部分添加一个 2 的列

届时您的订单将变为

order by newcolumn, organization.name

但是,您查询可能会返回公司两次

所以也许这些方面的东西可能更相关

SELECT weakMatch.bold_id,
   weakMatch.displayvalue,
   weakMatch.name
FROM   (SELECT DISTINCT department.bold_id,
                    departmentid + ' ' + organization.name + ' - '
                    + department.name AS DisplayValue,
                    organization.name
    FROM   department,
           organization
    WHERE  organization.bold_id = department.organization
           AND Upper(searchkeysname) LIKE '%[[]RG TRANSPORT%]%') AS
   directMatch
   LEFT OUTER JOIN (SELECT DISTINCT department.bold_id,
                                    departmentid + ' ' + organization.name +
                                    ' - '
                                    + department.name AS DisplayValue,
                                    organization.name
                    FROM   organization,
                           department
                    WHERE  organization.bold_id = department.organization
                           AND Upper(searchkeysname) LIKE '%[[]%RG%]%'
                           AND Upper(searchkeysname) LIKE
                               '%[[]%TRANSPORT%]%') AS
          weakMatch
                ON weakMatch.bold_id = directMatch.bold_id
ORDER  BY directMatch.name,
      weakMatch.name 

尽管这假设总是在弱匹配中找到直接匹配

于 2013-09-04T13:22:42.897 回答