Php 如何在MySQL中获得类似的百分比记录?

Php 如何在MySQL中获得类似的百分比记录?,php,mysql,Php,Mysql,我有两张桌子。我想得到类似的百分比记录 select u1.id, count(ut2.tag) matches, count(ut1.tag) total, count(ut2.tag) / count(ut1.tag) pct from users u1 inner join user_tags ut1 on u1.id = ut1.user_id left join users u2

我有两张桌子。我想得到类似的百分比记录

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
请看我的代码:

使用者

用户标签

+-----+---------+------------+
| id  | user_id | tag        |
+-----+---------+------------+
|  1  |  1      | php        |
|  2  |  1      | java       |
|  3  |  2      | java       |
|  4  |  1      | dotnet     |
|  5  |  2      | oracle     |
|  6  |  3      | oracle     |
|  7  |  3      | python     |
|  8  |  4      | sql        |
|  9  |  3      | java       |
+-----+---------+------------+
有两个表
用户
用户标签
我需要3条记录。用户id 1有3个标签
php、java、dotnet
user id 2有2个标签,user id 3有3个标签,4有1个标签

假设我的查询基于users表中的id=2。标签表上有2个标签。我需要这两个类似于其他用户标签的标签

用户id=2

tags=java,oracle


用户id=1

tags=php、java、dotnet(匹配标记java存在-百分比应为匹配标记数/总标记数)表示1/3


用户id=3

tags=oracle、python、java
2个标记匹配(java和oracle)
百分比应为2/3


用户id=4,标记=sql(没有匹配的标记,因此百分比为0%)


如何使用MySQL和php代码来管理这个问题?

我们可以通过将所有用户的标签加入到用户2的标签中来得到您想要的答案。然后,我们只需计算适当的值就可以得到您的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
如您所见-如果左侧用户的标记与右侧用户的标记不匹配,则输入空值。mysql中的
count
函数忽略空值,不计算它们。因此,当我们按u1.id(最左边的id值)分组并计算它们拥有的标记数(
ut1.tag
)时,我们得到了它们所有标记的总数。然而,当我们计算
ut2.tag
的值时,我们只得到不为
null
的总数,即,只得到匹配的总数。这可以让我们得到你的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
编辑以供评论

您在注释中添加了能够同时与多个用户进行比较的要求,因为这意味着它可能会多次匹配同一标记,所以我们只需要计算不同的元素

select u1.id, 
       count(distinct ut2.tag) matches, 
       count(ut1.tag) total, 
       count(distinct ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id IN (2,3)
  where u1.id NOT IN (2,3)
  group by u1.id
  having count(ut2.tag) > 0;

通过更新的fiddle

我们可以得到您想要的答案,方法是左键将所有用户与其标签连接起来,左键将用户2与其标签连接起来。然后,我们只需计算适当的值就可以得到您的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
如您所见-如果左侧用户的标记与右侧用户的标记不匹配,则输入空值。mysql中的
count
函数忽略空值,不计算它们。因此,当我们按u1.id(最左边的id值)分组并计算它们拥有的标记数(
ut1.tag
)时,我们得到了它们所有标记的总数。然而,当我们计算
ut2.tag
的值时,我们只得到不为
null
的总数,即,只得到匹配的总数。这可以让我们得到你的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
编辑以供评论

您在注释中添加了能够同时与多个用户进行比较的要求,因为这意味着它可能会多次匹配同一标记,所以我们只需要计算不同的元素

select u1.id, 
       count(distinct ut2.tag) matches, 
       count(ut1.tag) total, 
       count(distinct ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id IN (2,3)
  where u1.id NOT IN (2,3)
  group by u1.id
  having count(ut2.tag) > 0;

通过更新的fiddle

我们可以得到您想要的答案,方法是左键将所有用户与其标签连接起来,左键将用户2与其标签连接起来。然后,我们只需计算适当的值就可以得到您的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
如您所见-如果左侧用户的标记与右侧用户的标记不匹配,则输入空值。mysql中的
count
函数忽略空值,不计算它们。因此,当我们按u1.id(最左边的id值)分组并计算它们拥有的标记数(
ut1.tag
)时,我们得到了它们所有标记的总数。然而,当我们计算
ut2.tag
的值时,我们只得到不为
null
的总数,即,只得到匹配的总数。这可以让我们得到你的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
编辑以供评论

您在注释中添加了能够同时与多个用户进行比较的要求,因为这意味着它可能会多次匹配同一标记,所以我们只需要计算不同的元素

select u1.id, 
       count(distinct ut2.tag) matches, 
       count(ut1.tag) total, 
       count(distinct ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id IN (2,3)
  where u1.id NOT IN (2,3)
  group by u1.id
  having count(ut2.tag) > 0;

通过更新的fiddle

我们可以得到您想要的答案,方法是左键将所有用户与其标签连接起来,左键将用户2与其标签连接起来。然后,我们只需计算适当的值就可以得到您的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
如您所见-如果左侧用户的标记与右侧用户的标记不匹配,则输入空值。mysql中的
count
函数忽略空值,不计算它们。因此,当我们按u1.id(最左边的id值)分组并计算它们拥有的标记数(
ut1.tag
)时,我们得到了它们所有标记的总数。然而,当我们计算
ut2.tag
的值时,我们只得到不为
null
的总数,即,只得到匹配的总数。这可以让我们得到你的百分比

select u1.id, 
       count(ut2.tag) matches, 
       count(ut1.tag) total, 
       count(ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id = 2
  where u1.id <> 2
  group by u1.id
编辑以供评论

您在注释中添加了能够同时与多个用户进行比较的要求,因为这意味着它可能会多次匹配同一标记,所以我们只需要计算不同的元素

select u1.id, 
       count(distinct ut2.tag) matches, 
       count(ut1.tag) total, 
       count(distinct ut2.tag) / count(ut1.tag) pct
  from users u1
    inner join user_tags ut1
      on u1.id = ut1.user_id
    left join
      users u2
        inner join user_tags ut2
          on u2.id = ut2.user_id
    on ut2.tag = ut1.tag
      and u2.id IN (2,3)
  where u1.id NOT IN (2,3)
  group by u1.id
  having count(ut2.tag) > 0;

使用更新的fiddle

如果您使用外键,问题很容易解决,而且没有大问题,我正在使用外键。如果您的表名
用户
,则
用户标签
表列应为
用户id
。检查一下我不确定您是否知道百分比是多少。否@Abdulla column name
user\u id
这是cakephp表模型关系。我需要mysql查询,然后我将转换成cakephp格式。如果您使用外键问题很容易解决,并且没有大的查询,我使用的是外键。如果您的表名
users
,则
users\u tags
表列应为
users\u id
。检查一下我不确定您是否知道百分比是多少。否@Abdulla column name
user\u id
这是cakephp表模型关系。我需要mysql查询,然后我将转换成cakephp格式。如果你使用外键的问题