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?
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
Related
i'm trying to run an SOQL query on salesforce to get all users and their permission set id.
it is important the list return will be by user not by permission sets, meaning if i have 1000 users i will get back 1000 records and for each record the user attributes like email etc + permission sets list of Id's assign to him
SELECT+id,PermissionSet.id+FROM+User i tried finding the relationship field name but i'm not so familiar wtih salesforce, please assist
https://developer.salesforce.com/docs/atlas.en-us.238.0.object_reference.meta/object_reference/sforce_api_erd_profile_permissions.htm
The table you're looking for is PermissionSetAssignment
Top-down:
select id, email,
(select permissionsetid
from permissionsetassignments
where permissionset.isownedbyprofile = false)
from user
or bottom-up
select assigneeid, assignee.email, permissionsetid
from permissionsetassignment
where permissionset.isownedbyprofile = false
order by assigneeid
I have a database containing all usernames. Now, I have a page where I can search for an user. At the moment I use the following SQL:
SELECT uid, username, id, status
FROM users
WHERE `username` LIKE <search string>
Now, I have a record with the username Hattorius. But when I use that SQL syntax, and search for hatt. It doesn't give any results. How can I still make this work?
I searched some around, but nobody really had an answer to this.
Try to use LIKE :
SELECT uid, username, id, status
FROM users
WHERE `username` LIKE '%hatt%'
Remove the single quotes:
SELECT uid, username, id, status
FROM users
WHERE username LIKE '%hatt%'
I have two tables. user(user_id, username, password, age) and comment(comment_id, comment, user_id(foreign key)).
I'm trying to get username from user, using the user_id provided in comment.
My query looks like this:
$sql = "SELECT username FROM user WHERE user_id = (SELECT user_id FROM comments)";
I'm getting null. Is my brain working poorly or is it something else I messed up?
I just want to display all comments after each other, with the username before it.
Use IN instead of "=" .
SELECT username FROM user WHERE user_id IN (SELECT user_id FROM comments);
OR you can use a proper join, something like:
SELECT username FROM user,comments WHERE user.user_id = comments.user_id
That's not a join - a join would be:
$sql = "SELECT username FROM user u JOIN comments c ON u.user_id = c.user_id";
When you use a subquery with =, the subquery must return one value. To show all related records in a related table, use JOIN instead.
I have tested the following query which is successful but when apply the query to my custom object it fails. i need to find out what is causing the issue.
SELECT Id, Account.Name FROM Contacts WHERE AccountId in (SELECT Id FROM Account)
Background: There are two entities: Customers and Visits, there can be many visits for any customer. I have created a custom object Customer_Visits__c with several fields. It has a lookup field called Customer__c which looks up to Account (sObject) and stores the Account Id field. The relationship name is CustomerVisitsAccounts. I want to be able to return the customer name field (Account.Name) in the SOQL query, i.e., for each visit record show the name of the customer.
It get success with:
SELECT Id, Name FROM Customer_Visits_c WHERE Customer_c in (SELECT Id FROM Account)
Here is the SOQL string I get fails with:
SELECT Id, Name, CustomerVisitsAccounts__r.Name FROM Customer_Visits_c WHERE Customer_c in (SELECT Id FROM Account)
Here is the error message I get:
ERROR at Row:1:Column:18
Didn't understand relationship 'CustomerVisitsAccounts_r' in field path. If you are attempting to use a custom relationship, be sure to append the '_r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
I have this view in an Oracle db and I'm trying to generate a report with an SQL query but it's not working for last few days now so decide to get expert advice.
View does runs well. Basically what view does is it records all users who log into any database for auditing purpose.
View columns:
dbname
username
server
program
logon_time
Questions:
I am trying to generate SQL to group by all username and program who log in to dbname=x on date=26-Dec-2012
count distinct username and program where user logged on 26-Dec-2012
Basically trying to get... if dbname=x get 50000 login via username=abc then abc was connected from which server and what program?
Tried the following query
select dbname, username, server, program, logon_time from audit
where username = abc and
dbname in (select dbname from audit
where dbname='x' and logon_time = to_date('26-DEC-2012','DD-MON-YYY'))
group by username, program
This seems very straight-forward, except for issues with reformatting the date+time information into just date information.
Query 1:
SELECT username, program
FROM Audit
WHERE dbname = 'x'
AND TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program;
This should give you a list of the distinct username, program combinations that logged into database 'x' on 26th December 2012.
Query 2:
You want the COUNT of the rows from Query 1 except for the criterion about database name, so:
SELECT COUNT(*)
FROM (SELECT username, program
FROM Audit
WHERE TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program
)
This should give you a count of the distinct user name, program combinations that connected to any database on 26th December 2012.
You could try something like this:
SELECT dbname, username, server, program, trunc(logon_time), count(*) FROM audit
WHERE username = 'abc'
AND dbname = 'x'
AND logon_time >= to_date('26-DEC-2012','DD-MON-YYY')
AND logon_time < to_date('27-DEC-2012','DD-MON-YYY')
GROUP BY dbname, username, server, program, trunc(logon_time)
Edit: Answer revised to fix grouping mistake Conrad pointed out.
Still not 100% sure what is required, but this should be much closer.