I have to make the following adaptation in a query:
Table app_usuários
: id, nome, sobrenome, hash, sexo, interesse, latitude e longitude
.
Table app_likes
: id, user_a_id, user_b_id
Briefly, I have a query that takes users based on gender and interest within a 25-kilometer radius.
What I need to do: I have to do the query not get users I already gave like accessing the app_likes
table, I know I have to do this with inner join
, but how? Consider that user_a_id
would be the id of who gave like.
My query:
$teste = $app->database->query("SELECT user_id, user_firstname, latitude, longitude,
(6371 * acos(
cos( radians(".$app->session->user_data[0]['latitude'].") )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(".$app->session->user_data[0]['longitude'].") )
+ sin( radians(".$app->session->user_data[0]['latitude'].") )
* sin( radians( latitude ) )
)
) AS distancia
FROM app_users ".$query."
HAVING distancia < 25
ORDER BY distancia ASC
LIMIT 4;", $array);
Where the $ query and $ array variables come from:
if($app->session->user_data[0]['user_interest'] == "m" || $app->session->user_data[0]['user_interest'] == "f"){
$query = "WHERE user_gender = :ug AND (user_interest = :uin OR user_interest = :reserved) not in (".$app->session->user_data[0]['user_id'].")";
$array = array("ug"=>$app->session->user_data[0]['user_interest'], "uin"=>$app->session->user_data[0]['user_gender'],"reserved"=>"fm");
} else if($app->session->user_data[0]['user_interest'] == "fm"){
$query = "WHERE user_interest = :uin OR user_interest = :reserved not in (".$app->session->user_data[0]['user_id'].")";
$array = array("uin"=>$app->session->user_data[0]['user_gender'],"reserved"=>"fm");
}