I'm trying to do a equi join but it isn't working. I have 2 tables: Table 1 - taxonomy_term_data with fields "tid,vid,name,description,format,weight etc"

Table -2 -taxonomy_term_hierarchy with fields "tid and parent"

I have sql query with me it is working fine. This is the query "select t1. *,t2.vid,t2.name from taxonomy_term_hierarchy t1, taxonomy_term_data t2 where t1.tid = t2.tid AND vid=2 limit 5;"

I just want to get tid,name,vid from taxonomy_term_data table and parent from taxonomy_hierarchy table in db_select query format

2 Answers 2


You can built any type of select query using db_select, you can see in detail on following url https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7.x


You can change query based on you requirement.

$query = db_select('taxonomy_term_hierarchy', 't1');
    $query->join('taxonomy_term_data', 't2', 't1.tid = t2.tid');
    $result = $query
      ->fields('t1', array('tid', 'field1', 'field2'))
      ->fields('t2', array('tid', 'vid', 'name'))
      ->condition('t2.vid', '2')

    foreach ($result as $row) {
      // Do something with $row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.