const friendIds = friends.rows.map((friend) => friend.friend_id);
console.log("list", friendIds); //list [ 50, 51 ]
const users = await pool.query(
"SELECT * FROM super_user WHERE user_id NOT IN(ARRAY[$1])",
[friendIds]
);
I want to query all users where the user_id does not equal any of the items in the array.
for some reason, even chat GPT is unable to give me a good solution
3
Answers
you can use
<> ALL()
By Using ALL
By Using ANY
In the doc, there’s 9.24. Row and Array Comparisons showing examples of this: online demo
In 9.19. Array Functions and Operators you can find it’s also possible to just
unnest()
the array and compare to the resulting list of elements or see if thearray_position()
of your elementis null
:Good to know what happens if you get a null on either side: