Reputation: 195
I have a user chat system that has 2 tables: users table and followers table. I am performing a search and i want to get users that i am following
the users
table has this as the primary key: user_id
and the followers
table has the following columns following_id follower_id is_typing active
How do i join the two tables to get my result?
I have been doing this:
function Wo_GetNearbyFriends($args = array()) {
global $wo, $sqlConnect;
if ($wo['loggedin'] == false || empty($args)) {
return false;
}
$options = array(
"offset" => false,
"gender" => false,
"name" => false,
"distance" => false,
"relship" => false,
"status" => false,
"fid_5" => false,
"fid_6" => false,
"fid_7" => false,
"fid_8" => false,
"limit" => 20
);
$args = array_merge($options, $args);
$offset = Wo_Secure($args['offset']);
$gender = Wo_Secure($args['gender']);
$name = Wo_Secure($args['name']);
$loc_distance = Wo_Secure($args['distance']);
$status = Wo_Secure($args['status']);
$relship = Wo_Secure($args['relship']);
$fid_5 = Wo_Secure($args['fid_5']);
$fid_6 = Wo_Secure($args['fid_6']);
$fid_7 = Wo_Secure($args['fid_7']);
$fid_8 = Wo_Secure($args['fid_8']);
$limit = Wo_Secure($args['limit']);
$unit = 6371;
$user_lat = $wo['user']['lat'];
$user_lng = $wo['user']['lng'];
$user = $wo['user']['id'];
$t_users = T_USERS;
$t_followers = T_FOLLOWERS;
$distance = 25;
$data = array();
$sub_sql = "";
if ($loc_distance && is_numeric($loc_distance) && $loc_distance > 0) {
$distance = $loc_distance;
}
if ($name) {
$name = Wo_Secure($name);
$sub_sql .= " AND (`username` LIKE '%$name%' OR `first_name` LIKE '%$name%' OR `last_name` LIKE '%$name%') ";
}
if (isset($status) && $status != false) {
if ($status == 1) {
$time = time() - 60;
$sub_sql .= " AND `lastseen` > '$time'";
} else if ($status == 0) {
$time = time() - 60;
$sub_sql .= " AND `lastseen` < '$time'";
}
}
if ($relship && in_array($relship, array_keys($wo['relationship']))) {
$sub_sql .= " AND `relationship_id` = '$relship' ";
}
if ($offset && is_numeric($offset) && $offset > 0) {
$sub_sql .= " AND `user_id` < '$offset' AND `user_id` <> '$offset' ";
}
if ($gender && in_array($gender, array_keys($wo['genders']))) {
$sub_sql .= " AND `gender` = '$gender' ";
}
if($fid_5 && is_numeric($fid_5) && $fid_5 > 0){
$sub_sql .= " AND `fid_5` = '$fid_5' ";
}
if($fid_6 && is_numeric($fid_6) && $fid_6 > 0){
$sub_sql .= " AND `fid_6` = '$fid_6' ";
}
if($fid_7 && is_numeric($fid_7) && $fid_7 > 0){
$sub_sql .= " AND `fid_7` = '$fid_7' ";
}
if($fid_8 && is_numeric($fid_8) && $fid_8 > 0){
$sub_sql .= " AND `fid_8` = '$fid_8' ";
}
$sql = "SELECT `user_id`, ( {$unit} * acos(cos(radians('$user_lat')) * cos(radians(lat)) * cos(radians(lng) - radians('$user_lng')) + sin(radians('$user_lat')) * sin(radians(lat))) ) AS distance FROM $t_users WHERE `user_id` <> '$user' {$sub_sql} AND `user_id` IN (SELECT `follower_id` FROM $t_followers WHERE `follower_id` <> {$user} AND `following_id` = {$user} AND `active` = '1') AND `user_id` IN (SELECT `following_id` FROM $t_followers WHERE `follower_id` = {$user} AND `following_id` <> {$user} AND `active` = '1') AND `lat` <> 0 AND `lng` <> 0 HAVING distance < '$distance' ORDER BY `user_id` DESC LIMIT 0, $limit ";
$query = mysqli_query($sqlConnect, $sql);
while ($fetched_data = mysqli_fetch_assoc($query)) {
$fetched_data['user_data'] = Wo_UserData($fetched_data['user_id']);
$fetched_data['user_data']['age'] = Wo_GetUserCountryName($fetched_data['user_data']);
$fetched_data['user_geoinfo'] = $fetched_data['user_data']['lat'] . ',' . $fetched_data['user_data']['lng'];
if ($fetched_data['user_data']['share_my_location'] == 1) {
$data[] = $fetched_data;
}
}
return $data;
}
Upvotes: 0
Views: 143
Reputation: 94914
Here is the mere join. Once you have the two user rows you can apply the distance calculation on their data.
select *
from t_users u1
join t_users u2
on u1.user_id < u2.user_id
and (u1.user_id, u2.user_id) in (select follower_id, following_id from t_followers)
and (u1.user_id, u2.user_id) in (select following_id, follower_id from t_followers);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f29e1d27055575385d4b28d777fd335c
Upvotes: 1