arash rahimi arash rahimi - 4 months ago 7
MySQL Question

How can I compare 2 tables in mysql and check the result

I have two tables, one of them is for friends of a person and another one is for channel member.

I want to check if one of my friends is one of the channel members. If so don't show his name, else show him.

$get_friend = "select * from friends where user_1_id='$user_id' AND friends_status=1";
$run_friend = mysqli_query($conn,$get_friend);

$select_members = "SELECT user_id from channel_members where channel_id='$channel_id'";
$run_members = mysqli_query($conn,$select_members);

Answer

Try this query :

select * from friends where user_1_id='$user_id' AND friends_status=1 AND user_2_id NOT IN(SELECT user_id from channel_members where channel_id='$channel_id')

This will select all your friends from friends table which are not a member of channel '$channel_id'

Comments