0

I have a members search function on my website. What it basically does is getting some info from the leden table and show it for each member found, except for yourself.

The $id is the person that's logged in.

Right now, this is my current query:

$sql = "
SELECT ldn.schermnaam, ldn.geboortedatum, ldn.userid, ldn.foto, ldn.oproep
FROM leden AS ldn
WHERE ldn.userid != $id
";

This works fine.

Now what I want is to also exclude members that are blocked by you. The blockedmembers table looks like this:

- ID
- Useridsender (the one that blocked the other one)
- Useridreceiver (the one that has been blocked)
- Creation date

Now I want to exclude the blocked members and don't show them in the result list.

I tried using INNER JOIN and LEFT JOIN, but haven't figured out how to get a good query. Anyone knows how my query should look like?

p.s. it's a dynamic query, so based on the user input AND statements are added at the end.

0

4 Answers 4

6
$sql = "
SELECT ldn.schermnaam, ldn.geboortedatum, ldn.userid, ldn.foto, ldn.oproep
FROM leden AS ldn
WHERE ldn.userid != $id AND ldn.userid NOT IN(SELECT Useridreceiver FROM blocktable WHERE Useridsender=$id)
";

this should be pretty close I'm using a sub select but it can also be done using an outer join I think

Sign up to request clarification or add additional context in comments.

2 Comments

I think any join can be used, or at least any different from INNER JOIN, if the table is empty by default
Since you are the first one...I'll thank you! Works great, thanks :D Can't upvote you yet. Also, thanks to the rest!
3

A lot of people are answering, using sub selects. Here's mine using LEFT JOIN (without subselect)

You can use LEFT JOIN to select matches, then extract out entries with no match (joined table id IS NULL)

SELECT ldn.schermnaam, ldn.geboortedatum, ldn.userid, ldn.foto, ldn.oproep
FROM leden AS ldn LEFT JOIN blockedmembers AS blm
  ON (blm.Useridsender=$id AND ldn.userid=blm.Useridreceiver)
WHERE ldn.userid != $id 
AND blm.ID IS NULL

1 Comment

+1 for providing the join based version since I'm too lazy to do it myself :)
1

Filter results from blockedmembers tables also

$sql="SELECT ldn.schermnaam, ldn.geboortedatum, ldn.userid, ldn.foto, ldn.oproep
FROM leden AS ldn
WHERE ldn.userid != $id AND ldn.userid NOT IN 
(SELECT Useridreceiver FROM blockedmembers WHERE Useridsender =$id)";

1 Comment

@RoyalBg now added the purpose
1

You have working answers using NOT IN however nobody has suggested NOT EXISTS, which could be a better performing option for you.

Take a look here: NOT IN vs NOT EXISTS

SELECT 
  ldn.schermnaam, ldn.geboortedatum, ldn.userid, ldn.foto, ldn.oproep
FROM 
  leden AS ldn
WHERE 
  ldn.userid != $id
AND
  NOT EXISTS (
    SELECT 1 
    FROM blockedmembers AS bm 
    WHERE 
      bm.Useridreceiver = ldn.userid
    AND
      bm.Useridsender = $id
  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.