I’m a SQL newbie having used ORMs most of the time. I am having trouble with aliases in the following query:
command.CommandText = @"
INSERT INTO permissions (userid, remoteid)
SELECT userid as uid, :remoteid as rid
FROM users
WHERE companyid = :companyid
AND NOT EXISTS (
SELECT 1
FROM permissions
WHERE userid = uid
AND remoteid = rid
)";
command.Parameters.AddWithValue("companyid", companyId);
command.Parameters.AddWithValue("remoteid", resourceId);
When the query executes, I get the following error:
ERROR: 42703: column "uid" does not exist
What am I doing wrong? Are aliases supposed to be used like this and can they be used in an INSERT statement?
how about this: