drgaeqdqiiyg14608 2015-05-29 15:38
浏览 33
已采纳

根据最长匹配组织搜索结果

I have a database table that contains project keywords. Each project has several keywords.

In the web application, a user can perform a search by entering multiple keywords to search for. If the user enters (for example) four keywords, I want to return the search result as a list of projects. The projects should be ordered based on the longest matches first. The first projects listed will the ones that matched on all four keywords, followed by projects that matched on three out of four keywords, etc.

What query can I write to return the results?

This is a rough idea of what I think the query would look like:

select projectID 
  from project_keyword 
 where keyword = '*keyword1*' 
    or keyword = '*keyword2*'
    or keyword = '*keyword3*'

But what I want is any projectID that matches all three keywords to be displayed first (if any), followed by the projectID that matched two out of three keywords, etc. projectID that don't have any matching keywords are not returned.

Example:

projectID  keyword 
---------  -------
 456       salsa
 456       guacamole
 456       tamale
 511       salsa
 511       tamale
 511       burrito
 511       taco
 654       margarita
 654       nachos
 789       margarita
 789       salsa  
 789       taco

For user keyword search for 'salsa','tamale','burrito', the query should return these projectID, in this order.

projectID
---------
 511          (matches all three)
 456          (matches two)
 789          (matches one)

How do I get the rows from the query returned in the specified order? (What query can I write to get that.)

  • 写回答

1条回答 默认 最新

  • duanheye7423 2015-05-29 19:09
    关注

    Something like this should work:

     SELECT k.projectID
       FROM project_keyword k
      WHERE k.keyword IN ('*keyword1*','*keyword2*','*keyword3*')
      GROUP BY k.projectID
      ORDER BY COUNT(DISTINCT k.keyword) DESC, k.projectID
    

    (This assumes that the table contains multiple rows for a projectID, one row for each keyword. If that's not how the table is organized, if it's a single row, and you are looking for matching keywords that occur somewhere in a column, we'd need a different query.)

    If you want to return the list of keywords that matched, you could use a GROUP_CONCAT aggregate in the SELECT list

     SELECT k.projectID
          , GROUP_CONCAT(DISTINCT k.keyword ORDER BY k.keyword) AS keywords_matched
       FROM ... 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面