php - 分析用户之间相互喜欢的表

标签 php mysql

在我的数据库中,我有一个表(喜欢),其中列出了用户喜欢的项目。
当访问用户个人资料时,我需要确定我们有多少共同的“喜欢”。
编写显示用户之间相互喜欢的查询的最佳方法是什么?

likes table

 id  |  user  |  item  | activated
-----------------------------------
 1   |  3     |  14    | 1          
 2   |  4     |  14    | 1


在此示例中我需要返回 14

最佳答案

假设应该是这样的:

select userViewer.item, items.itemName
from likes userViewer,
     likes userProfile,
     items
where userProfile.user = $profileUserId
  and userViewer.user = $userViewer
  and userProfile.item = userViewer.item
  and items.item = userViewer.item

其中 $profileUserId - 个人资料用户的 userId 和 $userViewer - 当前用户的 userId

对“join on”表单与“items”表进行相同的查询,例如:

select userViewer.item, items.itemName
from likes userViewer inner join likes userProfile 
on userProfile.user = $profileUserId
  and userViewer.user = $userViewer
  and userProfile.item = userViewer.item
inner join items 
on userViewer.item = items.item

关于php - 分析用户之间相互喜欢的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890757/

相关文章:

mysql - MYSQL中有 `on duplicate key insert`吗?

php - MySQL - 在字段中存储小数组的好方法

php - 如何使用 php 自动抓取网页?

php - 出错而不是使用 SSH 请求密码

php - PHP 中 objectAtIndex 的等效项

mysql - 创建 Oracle DBlink 以从 MySql 获取数据

php - 1 天后自动删除行不会使用 php

php - 如何在 PHP 中使用正则表达式删除前导字符和查询字符?

MySQL选择用户所在的地方

mysql - 什么时候在mysql中使用外键