You need to sign in to do that
Don't have an account?
LIMIT key word in subquery
Hi All
I am tryign to use like this. Can I Use this.
Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.
How Can I use Limit With in the SubQuery Is it possible.
Regards
Venkat
Hi
List<Account> acc=[Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.];
The size of the above limit may be above 5 since it contains record too but it will show exactly 5 when you try this
Set<Id> accIds = new Set<Id>();
List<Account> accList = [select id from Account];
for(Account accRec : accList){
accIds.add(accRec.Id);
}
List<Contact> conList = [select id,name,CreatedDate from Contact Where AccountId In : accIds order by CreatedDate DESC Limit 5];
Try to process contacts of each account with 2 for loops
Kindly let me know incase you need more assistance on this .
All Answers
Hi,
Yes u can use LIMIT keyword in subquery,But in your query there is one another problem,U wrote the wrong childRelationShip name of contact to Account.
The chidRelatiobShipname is 'Contacts' NOT contact.
Select id, Name, (Select id,name,CreatedDate from Contacts ORDER BY CreatedDate LIMIT 5) from Account.
otherwise the query is perfect .
Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.
Hi Venkat,
This is possible, because you are selecting all the contacts records in the query so there may be more than a record. So, you can limit only fist 5 should be queried after ordering it.
But a slight change is there, you need to mention the subquery object's name in plural.
So your query look like,
Select id, Name, (Select id,name,CreatedDate from Contacts ORDER BY CreatedDate LIMIT 5) from Account
Hope this helps you...!
Hi All,
Thanks for reply. Yes I know that contacts. But I am getting soem wrong result. I am getting Contact List is 8 but limit is 5.
May I know why It is.
Regards
Venkat
Hi
List<Account> acc=[Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.];
The size of the above limit may be above 5 since it contains record too but it will show exactly 5 when you try this
Set<Id> accIds = new Set<Id>();
List<Account> accList = [select id from Account];
for(Account accRec : accList){
accIds.add(accRec.Id);
}
List<Contact> conList = [select id,name,CreatedDate from Contact Where AccountId In : accIds order by CreatedDate DESC Limit 5];
Try to process contacts of each account with 2 for loops
Kindly let me know incase you need more assistance on this .
HI,
Thanks for your reply. Actually I miss understood that query. What ever your are saying that is correct
Thanks & Regards
Venkat