I need an equivalent SOQL query to this SQL query:
SQL query:
SELECT id, username, (SELECT username FROM users where id = u.manager_id) as manager_username
FROM users u
I have tried
SELECT Id, Username, (SELECT Username FROM User WHERE Id=usr.ManagerId) FROM User usr
What I keep getting is
[
{
"message": "\n(SELECT Username FROM User WHERE Id=usr.ManagerId) FROM User usr WHERE\n
^\nERROR at Row:1:Column:57\nBind variables only allowed in Apex code",
"errorCode": "MALFORMED_QUERY"
}
]
Any suggestions?
CodePudding user response:
SOQL doesn't support field comparison. But in your case, it's not needed. The manager is the parent record, so its fields can be accessed via dot notation. Your query can just be:
SELECT Id, Username, Manager.Id, Manager.Username FROM User