我有一个搜索字段,在这种情况下用户搜索“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行。所以我希望它在第一行。 然后我尝试使用union
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%]%'
它工作正常但后面的行没有按名称排序。 如果我添加
ORDER BY organization.name
到最后一个查询第一行丢失。 我还有其他办法吗?
答案 0 :(得分: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
虽然这假设直接匹配总是在弱匹配中找到