4
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 picks users based on sex and interest in a radius of 25 kilometers.
What I need to do: I have to make the query not catch users I’ve given like accessing the table app_likes
, 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");
}
I updated the question and put where the $query and $array variables come from
– Paula Torrens
Edited response to consider variable content
$query
.– João Martins
John, thank you for your help. But I don’t think you got it right. Thus, if user_b_id is equal to the id of the record that came from the query, it would not be included in the result.
– Paula Torrens
Said the camp
user_a_id
was the field that should link to the tableapp_likes
, and if so, connecting the two tables with theLEFT JOIN
and saying that we do not want the records of users who have already given like (have a record in the tableapp_likes
), then I guess that’s what I was asking, right?– João Martins
Come on. I’ll try to explain using algorithm. [Select 4 users in a radius of 25 kilometers, also check if any user has been returned where I have given like using the app_likes table, whereas user_a_id is my id and user_b_id is the id of other users, if it is found do not return these results]
– Paula Torrens
I simply want the query to return users with the requirements defined in WHERE and the query to delete users I have already given like, using the app_like table. Whereas user_a_id is my id and user_b_id is the id of the user I gave like to.
– Paula Torrens
Okay, it’s tough. What’s being returned on query initial?
– João Martins
The $query variable sends the user preferences for the query.
– Paula Torrens
If the user is bisexual, have men and women selected who are interested in his gender. If the user is straight, select the opposite sex that is interested in his gender. This is how the $query variable works. The $array variable contains the data of these preferences.
– Paula Torrens
Consider that $app->Session->user_data[0] contains the user data that is logged in
– Paula Torrens
I think I’ve got the set! I edited the answer to
JOIN
capture the results in which the user represented byuser_b_id
has been Likes and at the same time that Likes come from the user who is logged in, then in the clauseWHERE
we say that all these results should be removed from the query.– João Martins
Man, you are awesome! It worked perfectly.
– Paula Torrens