Drupal Join Query with entityCondition
This is the best solution for you ==> https://www.drupal.org/node/1343708

EFQ an beautifull tools for manipulate the entities in drupal.

Do not hesitate if you need precision but this doc is verry good.

In the Entity Reference case 

The column name for an entity reference field is target_id, so your code should be:

$query = new EntityFieldQuery();

$query
  -&gt;entityCondition('entity_type', 'node', '=')
  -&gt;propertyCondition('type', 'custom_node_type', '=')
  -&gt;fieldCondition('field_reference_name', 'target_id', $id, '=');

$entities = $query-&gt;execute();


For follow the relation of your entities you need to reference the A into B or B into A with a reference field. When you use EFQ to search the relation in node A with B you need to use fieldCondition in this reference field in my example is field_reference_name. If you list the content B (ids) into an array and you user this

$query = new EntityFieldQuery();

$query
  -&gt;entityCondition('entity_type', 'node', '=')
  -&gt;propertyCondition('type', 'a', '=')

$entities_a = $query-&gt;execute();


For search the relation A to B into A you need to use this : 

$query = new EntityFieldQuery();

$query
  -&gt;entityCondition('entity_type', 'node', '=')
  -&gt;propertyCondition('type', 'b', '=')
  -&gt;fieldCondition('field_reference', 'target_id', $entities_a, 'IN');

$entities_b_refered_into_a = $query-&gt;execute();


Good chance.