Search code examples
phpmysqldatabasedrupaldrupal-7

Join DB tables on Drupal


i have a watchdog table and i create a distinct table which i have the unique variables from watchdog with their wids as key. What i want is to join the 2 tables (distinct and watchdog) in order to join all the values with unique variables. i did this:

$query = db_select('distinct', 'di');
    $query -> join('watchdog', 'wa', 'di.wid = wa.wid');
    $query -> fields('u', array('variables', 'type', 'severity','message', 'wid', 'timestamp'));
    $result = $query->execute();
  }

I cannot find where is my fault


Solution

  • Your field alias u doesn't exist. You declared di and wa but not u. Change this to wa, I suppose the selected columns are from the watchdog table.

    $query = db_select('distinct', 'di');
    $query->join('watchdog', 'wa', 'di.wid = wa.wid');
    $query->fields('wa', array('variables', 'type', 'severity','message', 'wid', 'timestamp'));
    
    $result = $query->execute();