简体   繁体   中英

SQL Search Query ,how to assign weight to each input parameters and how to order the results by weight of input parameters,

I have a table [Car] with CarID,Make,Mode,Version,State,City,MinPrice,Maxprice, as 8 columns. I want to implement a search algorithm with 4 input parameters as Make,City,MinPrice,MaxPrice

How to assign weight to each input parameter.

Please advise me to write search query which gives AND results first and then OR results to follow AND results based on the weight of 4 input parameters.

Try something like:

select * from [Car]
where Make = @Make or 
      City = @City or 
      MinPrice >= @MinPrice or 
      MaxPrice <= @MaxPrice
order by 
      case when Make = @Make then 3 else 0 end + 
      case when City = @City  then 2 else 0 end + 
      case when MinPrice >= @MinPrice then 1 else 0 end + 
      case when MaxPrice <= @MaxPrice then 1 else 0 end
      desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM