I have SQL Query and I want to execute it in Drupal. My SQL Query is like this.
select cat.tid as cat_tid, cat.name as cat_name, cat_parent.parent as cat_parent_tid,
count(deals.nid) as no_of_deals
from
taxonomy_term_data as cat
left outer join taxonomy_term_hierarchy as cat_parent on cat_parent.tid=cat.tid
left outer join field_revision_field_categories as store_categories on
store_categories.field_categories_tid=cat.tid
left outer join field_revision_field_store as deal_stores on field_store_nid=store_categories.entity_id
left outer join (select * from node where type='deal' and status=1) as deals on nid=deal_stores.entity_id
where cat.vid=5
group by cat.tid, cat.name, cat_parent.parent
I converted this query with the help of this module https://drupal.org/project/query_coder
and got the resulted query as
$query = db_select('taxonomy_term_data', 'cat');
$query->addExpression('count(deals.nid)', 'no_of_deals');
$query->fields('cat', array('tid', 'name'));
$query->fields('cat_parent', array('parent'));
$query->leftJoin('taxonomy_term_hierarchy', 'cat_parent', 'cat_parent.tid = cat.tid');
$query->leftJoin('field_revision_field_categories', 'store_categories', 'store_categories.field_categories_tid = cat.tid');
$query->leftJoin('field_revision_field_store', 'deal_stores', 'field_store_nid = store_categories.entity_id');
$query->leftJoin('', 'deals', 'nid = deal_stores.entity_id');
$query->condition('cat.vid', 5);
$query->groupBy('cat.tid');
$query->groupBy('cat.name');
$query->groupBy('cat_parent.parent');
$result = $query->execute();
Now how can I execute this query in drupal, I tried to import this query in views 3 but got some unexpected error, Also I tried to add this query into the block but again getting the same error. I also created a custom block in a custom module that too did not worked. Please help me how can I execute this query. Answer will be greatly appreciated.
$query->leftJoin('', 'deals', 'nid = deal_stores.entity_id');
there is no table definition. First argument should be table name.