where is the fault in my join? $query = db_select('watchdog', 'wa');
$query-&gt;join('distinct', 'di', 'di.wid == wa.wid')
  -&gt;orderBy('wid')
  -&gt;fields('wa', array('variables', 'type', 'severity', 'message', 'wid',
    'timestamp'))
  -&gt;range(0, 20);
$result = $query-&gt;execute();


It shows me the error: Call to a member function orderBy() on a non-object in
You have join the distinct table with watchdog. So the all result of watchdog will be displayed and the matching record of distinct table. So try changing query like : 

$query = db_select('distinct', 'di');
$query-&gt;join('watchdog', 'wa', 'di.wid == wa.wid')
  -&gt;orderBy('wid')
  -&gt;fields('wa', array('variables', 'type', 'severity', 'message', 'wid',
    'timestamp'))
  -&gt;range(0, 20);
$result = $query-&gt;execute();