如何让多关键字搜索出来的结果按输入关键字的先后顺序来排序?
比如说,我搜索时输入的关键字是 "CC AA BB ",那么,在输出结果的时候,要求先输出包含CC的,然后输出包含AA的,最后是包含BB的,,就是说是按输入关键字的先后顺序来排序,这样子如何实现?
我用的是ACCESS,下面是我现在搜索用的代码:
keyword=request.Form( "keyword ")
keyword=Ltrim(Rtrim(keyword))
if instr(keyword, " ") <> 0 then
str = split(keyword, " ")
for i=0 to ubound(str)
if i=0 then
sqlstr = sqlstr& " [a] like '% "&str(i)& "% ' "
else
sqlstr = sqlstr& " or [a] like '% "&str(i)& "% ' "
end if
next
sqlstr= "select * from pros where "&sqlstr
sqlstr =sqlstr & " order by id "
else
sqlstr= "select * from pros "
sqlstr = sqlstr & " where [a] like '% " & keyword & "% ' "
sqlstr =sqlstr & " order by id "
end if
------解决方案--------------------如果你想对多列排序的话,那么在列与列之间要加上逗号,比如 :
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales '
ORDER BY salary, age DESC;
所以你只需要把你的关键字组合成一个字符串
strKey = key(1) & ", " & key(2) ....
然后放入sql语句中即可
------解决方案--------------------可以在查询之间使用 UNION 运算符,以将查询的结果组合成单个结果集。
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales ' and a like '%ccc% '
unit
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales ' and a like '%ccc% '
不知道这样行不行?