---
title: "查询匹配度"
author: "e_e"
date: "2016-11-10T10:49:10Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2016-12-06T16:30:46Z"
id: 2336910
url: "https://e-e.iteye.com/blog/2336910"
---

产品智能推介。找匹配度最高的记录。

 

select *  

from(select id,org\_id,sum(  

(case when instr(product\_type,'2')&gt;0 then 1 else 0 end)  

+(case when interest&gt;5 and interest&lt;11 then 1 else 0 end)  

+(case when 2000 between amount\_low and amount\_high then 1 else 0 end)  

)  

as cnt from hideseek.OrgProduct group by id) as temp  

where cnt&gt;0  

order by cnt desc,id asc  

 

 

 

//按照产品名称分组，每组取匹配度最高的记录出来

select * from (

select 

    *

from

    (select 

        p . \*,

            logo.org\_logo,

            sum((case

                when instr(product\_type, '1') &gt; 0 then 1

                else 0

            end) + (case

                when interest between 1 and 1 then 1

                else 0

            end) + (case

                when instalment between 2 and 12 then 1

                else 0

            end) + (case

                when mobile &lt;= 0 then 1

                else 0

            end) + (case

                when e\_business &lt;= 0 then 1

                else 0

            end) + (case

                when housing\_fund &lt;= 0 then 1

                else 0

            end) + (case

                when social\_insurance &lt;= 0 then 1

                else 0

            end)) as matchingDegree

    from

        tradeplatform.OrgProduct p

    left join tradeplatform.OrgLogo logo ON p.id = logo.pro\_id

    where

        p.plat\_status = 1 and product\_type = '1'

    group by id) as temp

where

    matchingDegree &gt; 0

        and org\_id not in ('juxinli')

 

order by matchingDegree desc 

) A GROUP BY product\_name ORDER BY matchingDegree DESC