I can’t really find any answer that fits my question.
I need to know how I can be able to use a subquery together with a custom value with the IN Operator in SQL. Here is an example of a query I came up with:
SELECT posts.content, users.username,users.name, users.verified FROM posts, users WHERE users.uid = posts.post_by AND posts.post_by IN ((SELECT user_uid FROM user_data.user1_following), 'USER_UID') ORDER BY posts.id DESC;
This query is meant to show posts only from users in a users’ following table – And it should also show posts from the original user themselves.
post_by represents a users UID(Unique ID)
This query works if the user is only following one person. If they follow multiple people, an error is returned. It states the following:
more than one row returned by a subquery used as an expression
I know this is coming from the block:
((SELECT user_uid FROM user_data.user1_following), 'USER_UID')
How do I go about getting the result I’m looking for by using both an subquery and a custom value (‘USER_UID’)? Alternative methods are welcome as long as they produce the result I’m looking for.
Advertisement
Answer
You can use UNION
inside your IN
clause as long as user_uid
has Character type:
IN (SELECT user_uid FROM user_data.user1_following UNION SELECT 'USER_UID')