我正在搜索我想保持自然顺序以保持排名的地方。这是我的代码查询:
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first = '{0}' AND last = '{1}' UNION
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE phone = '{6}' UNION
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first LIKE '{0}' AND last LIKE '{1}' UNION
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE address LIKE '{2}' AND city LIKE '{3}' UNION
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first = '{0}' OR last = '{1}' OR address = '{2}' OR city = '{3}' OR state = '{4}' OR zip = '{5}' OR phone = '{6}' or email = '{7}'"
联合中的第一个选择返回一个完全匹配,而最后一个选择返回一个非常普遍的匹配。问题是结果总是按buyer_id 排序返回。我无法更改现有的表/索引,并且我看到有几个索引,buyer_id 是第一个。如果可能,我想“禁用”索引顺序,以便首先返回第一个选择的结果,随后的选择实际上附加到前一个。这可能吗?
PS默认情况下,联合是不同的,如果我为每个选择添加一个“排名”列并按此排序,它将留下重复的结果,因为每个选择查询之间的排名列是不同的。