1

Hi I am currently coding something which requires an if before the results are retrieved from the database.

The case is that if the interacterID is not NULL then filter by it, see below

SELECT *
FROM status
WHERE type != "3"
AND userID = "'.$userID.'"
AND 
(
    interacterID IN ( #IF interacterID is null I want to skip this AND
        SELECT userID
        FROM userFriends
        WHERE (
            userFriends.userID = "'.$currentUserID.'"
            OR userFriends.interacterID = "'.$currentUserID.'"
        )
        AND status = "1"
    )
)
ORDER BY addedDate DESC
LIMIT 1

Thanks in advance for the help.

2 Answers 2

2

If I'm reading this right, I'm not sure you need an IF here (which can be done). A simple IS NULL and OR can be used for the two cases:

SELECT *
FROM status
WHERE type != "3"
AND userID = "'.$userID.'"
AND 
(
    interacterID IS NULL OR interacterID IN (
        SELECT userID
        FROM userFriends
        WHERE (
            userFriends.userID = "'.$currentUserID.'"
            OR userFriends.interacterID = "'.$currentUserID.'"
        )
        AND status = "1"
    )
)
ORDER BY addedDate DESC
LIMIT 1    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, can't believe missed that, awesome working like I need it now, thanks for the help! When the 5 minutes is up will accept your answer.
1
SELECT *
FROM status
WHERE type != "3"
AND userID = "'.$userID.'"
AND 
(
interacterID IS NOT NULL  (
    SELECT userID
    FROM userFriends
    WHERE (
        userFriends.userID = "'.$currentUserID.'"
        OR userFriends.interacterID = "'.$currentUserID.'"
    )
    AND status = "1"
)
  )
  ORDER BY addedDate DESC
  LIMIT 1  

If interacterID is null then the code will not executed

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.