0

When I try this query:

SELECT * 
FROM sds_posts 
WHERE topic_id = '2439' 
  AND author = ANY (SELECT mid 
                    FROM sds_actions 
                    WHERE whoami = '710' AND type = 'block')
  AND status = '1' 
  AND deleted = '0' 
ORDER BY 
  id ASC 
LIMIT 50

it is working correctly.

But I need this one:

SELECT * 
FROM sds_posts 
WHERE topic_id = '2439' 
  AND author <> ANY (SELECT mid 
                    FROM sds_actions 
                    WHERE whoami = '710' AND type = 'block')
  AND status = '1' 
  AND deleted = '0' 
ORDER BY 
  id ASC 
LIMIT 50

This time query have to select opposite of first query, but it is just select all author. I tried != and also NOT IN, but result is same.

So why? Why does <> not work as expected?

1
  • If the "any" works, maybe inverting the inner select would work: SELECT * FROM sds_posts WHERE topic_id='2439' and author = any (SELECT mid FROM sds_actions WHERE whoami<>'710' OR type<>'block') AND status='1' and deleted='0' ORDER by id ASC LIMIT 50 Commented Nov 25, 2014 at 4:07

2 Answers 2

2

I would think that changing

and author = any...
to
and NOT author = any...

would work... But if that does not, then I would try doing as a left-join and looking for null. Since the author is the "mid" from the sds_actions, I would write it as...

SELECT 
      sp.* 
   FROM 
      sds_posts sp
         LEFT JOIN sds_actions sa
            on sp.author = sa.mid
           AND sa.whoami = '710'
           AND sa.type = 'block'
   WHERE 
          sp.topic_id = '2439' 
      AND sp.status = '1' 
      AND sp.deleted = '0' 
      AND sa.mid IS NULL
   ORDER by 
      sp.id ASC 
   LIMIT 50
Sign up to request clarification or add additional context in comments.

Comments

0

You can try change author = any(...) to author IN (...)

and change author <> any(...) to author NOT IN (...)

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.