字符串列表之间的百分比匹配(多个交叉点)

时间:2014-03-28 17:50:01

标签: mysql

我有一个(list_id,string)表,其中包含字符串列表。我需要计算列表a与其他列表匹配的百分比:

lista = ["a","b","c"] # Desired result:
list1 = ["b","c","g"] # 66%
list2 = ["g","h","i"] #  0%

以下通过执行两个列表的交集来获得针对第一个列表的完成百分比。如何编写此声明以匹配第一个列表与所有其他列表:[" 66%"," 0%",]?

SELECT concat(round(( a.cnt1/b.cnt2 * 100 ),2),'%') AS percentage
FROM (SELECT count(string) AS cnt1 FROM tbl WHERE id=2 and (string) IN (SELECT string     FROM tbl where id=1)) a,
     (SELECT count(string) AS cnt2 FROM tbl WHERE id=1) b;

Here is the SQLFiddle

2 个答案:

答案 0 :(得分:1)

这样的事情是否足够(通常应该忽略数据层的表示事项,例如舍入并与%字符连接):

SELECT   a.id, COUNT(b.string)/COUNT(*) AS percentage
FROM     tbl a LEFT JOIN tbl b ON b.id = 1 AND a.string = b.string
GROUP BY a.id

根据您的要求,您可能还希望过滤a.id <> b.id

如果您绝对需要所描述的输出格式,则可以放入子查询并再次与GROUP_CONCAT()聚合:

SELECT GROUP_CONCAT(CONCAT(ROUND(percentage*100,2),'%')) FROM (
  SELECT   COUNT(b.string)/COUNT(*) AS percentage
  FROM     tbl a LEFT JOIN tbl b ON b.id = 1 AND a.string = b.string
  GROUP BY a.id
) t

答案 1 :(得分:0)

你可以用这个:

SELECT
  dest.id AS other_list,
  COUNT(dest.`string`)/(SELECT count(*) FROM tbl WHERE id=1)*100 as percentage_match
FROM
  tbl src
LEFT JOIN
  tbl dest
ON
  src.`string` = dest.`string` AND
  src.id = 1
WHERE
  dest.id != 1
GROUP BY
  dest.id

只需用列表替换1的每个出现,您想要与之比较。

根本没有显示没有匹配的列表。 (它也排除了匹配1对1,因为结果将是100%ofc。)

OTHER_LIST | PERCENTAGE_MATCH
2 | 66.6667 
4 | 33.3333 

http://www.sqlfiddle.com/#!2/c89012/49

无论列表有多少元素,它都可以工作。如果长度不同,甚至可以工作。 (但如果列表other - 在您的示例中说明 - 元素a,b,c,a,b,c将返回匹配&gt; 100%:http://www.sqlfiddle.com/#!2/e6c90/1