How to create dynamic query for inner join on same table?
$query = db_select('table1','t1');
$query-&gt;join('table2','t2','t1.id1=t2.id2');
$query-&gt;fields('t1', array('some fields'))
      -&gt;fields('t2', array('some fields'));
$result = $query-&gt;execute()-&gt;fetchAll();


Then you can do print_r stuff into $result like

echo "&lt;pre&gt;";print_r($result);echo "&lt;/pre&gt;";


If you have devel module installed and enabled, then you can also use:-

dsm($result);


This will give the whole result in form of array.
If you need in form of object, then you need to modify few lines of code like this:-

$data = $result-&gt;execute();
while($result = $data-&gt;fetchObject()){
  //Do your related stuff
}


I think it will help a lot. Thanks.