Relationship queries traverse parent-to-child and child-to-parent relationships between objects to filter and return results. They are similar to SQL joins. The relationship queries in SOQL must traverse a valid relationship path as defined in the rest of this section. There are 2 scenarios with relationship queries.
Child to Parent
Another way of writing this query
Parent to Child
Child to Parent
public class SOQLController
{
    public List getContacts()
    {
            List l = [SELECT c.Id, c.FirstName, c.LastName, a.Id, a.Name, c.AccountId
            FROM Contact c, c.Account a Limit 1];
            return l;
    }
}
Another way of writing this query
public class SOQLController
{
      public List getContacts()
      {
              List l = [SELECT c.Id, c.FirstName,c.LastName, c.Account.Name
        FROM Contact c Limit 1 ];
       
        return l;
      }
     
}
Parent to Child
public class SOQLController
{
      public List getAccountContacts()
      {
              List l = [select a.Name,(select c.LastName, c.FirstName from a.Contacts c) from Account a Limit 10];
     
        return l;
      }
     
}
 
No comments:
Post a Comment