我正在制作一个 iOS 应用程序,它利用类似于 Tinder 的滑动/匹配功能。我有一个 MySQL 数据库,用于将用户与符合用户搜索条件的人进行匹配。
滑动功能。 ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ 我的用户的搜索条件。 我的用户表基础是什么样的。
我使用 php 和 SQL 创建了一个 API 端点,以便在发生更改时更新每个用户的信息,并获取所有满足用户搜索条件的附近用户。
这是获取附近用户的 php 的样子。
$uid = strval($_GET['uid']);
$lat = floatval($_GET['lat']);
$lng = floatval($_GET['lng']);
$minAge = intval($_GET['minAge']);
$maxAge = intval($_GET['maxAge']);
$gender = intval($_GET['gender']);
$radius = intval($_GET['radius']);
$type = intval($_GET['type']);
// This SQL statement selects users from the table 'users' so long as
//they are within x miles of the user's location and within the age
//range and gender requirements
$sql = "SELECT *,
(((acos(sin((?*pi()/180)) * sin((`lat`*pi()/180))+cos((?*pi()/180)) * cos((`lat`*pi()/180)) * cos(((?- `lng`)*pi()/180))))*180/pi())*60*1.1515)
AS distance
from users having distance<? AND gender=? AND age<? AND age>? AND type=? AND uid<>?";
//Bind parameters to the placeholders
mysqli_stmt_bind_param($stmt, "dddiiiiis", $lat, $lat, $lng, $radius, $gender, $maxAge, $minAge, $type, $uid);
//Run parameters inside database
mysqli_stmt_execute($stmt);
发出此请求的 URL 可能如下所示:
https://mywebsitename.com/getNearbyUsers.php?uid=4pW2I1gpq5VHCHEdm0aEfTRgJO92&lat=38.461822509765625&lng=-90.32567239374883&minAge=18&maxAge=100&gender=2&radius=50&type=3
我的问题:
现在,每当用户(user1)在另一个用户(user2)上向右或向左滑动时,我想跟踪该 user2 是否已呈现给 user1,以便将来不会呈现它们。
我最初计划做什么:
解决这个问题的第一个想法是在 SQL 数据库中创建另一列,该列本质上是一个 uid 数组,用于传达哪些用户已显示给当前用户。每当前端用户 (user1) 向右或向左滑动另一个用户 (user2) 的个人资料时,user2 的 uid 就可以添加到 user1 已滑动的用户数组中。然后,我可以在 SQL 语句中指定仅返回不在当前用户的“alreadyPresentedWith”uid 列中的用户。
问题: 我研究了在 SQL 中引用大量值,发现关系数据库中的每一列不应有多个值。然而,根据我的技能和经验,这是我能想象完成这项任务的唯一方法。
我只是希望得到更有经验的 php/SQL 开发人员的反馈,了解哪个方向最适合我解决此类问题。
最佳答案
您应该有一个表格,例如swipes
,每个操作一行。说:
create table swipes (
swipes_id int auto_increment primary key,
swiper_uid binary(16),
swipee_uid binary(16),
action varchar(5), -- 'left', 'right', 'none'
created_at datetime default now(),
foreign key (swiper_uid) references persons(uid),
foreign key (swipee_uid) references persons(uid)
);
从OP编辑 所以,解决方案很简单。我按照建议创建了滑动表,创建了一个 API 端点,每次滑动发生时都会在表中插入一个“滑动”(在我的例子中,我们不需要存储用户是向左还是向右滑动),然后更新我的端点中获取附近用户的 SQL 语句。
在 SQL 语句中,我添加了一个 NOT EXISTS
检查来引用刷卡表,并检查某个条目是否将当前用户作为“刷卡者”以及查询返回的用户作为“刷一下。”这就是 php 文件中获取附近用户的准备好的语句现在的样子。
$sql = "SELECT *,
(((acos(sin((?*pi()/180)) * sin((`lat`*pi()/180))+cos((?*pi()/180)) * cos((`lat`*pi()/180)) * cos(((?- `lng`)*pi()/180))))*180/pi())*60*1.1515)
AS distance
from users having distance<? AND gender=? AND age<? AND age>? AND type=? AND uid<>? AND NOT EXISTS(SELECT NULL
FROM swipes s
WHERE s.swiperUid=? AND u.uid=s.swipeeUid)";
//Bind parameters to the placeholders
mysqli_stmt_bind_param($stmt, "dddiiiiiss", $lat, $lat, $lng, $radius, $gender, $maxAge, $minAge, $type, $uid, $uid);
关于php - 在 SQL 查询中传递大量值的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915180/