I am trying to run a subquery through views and found this call. I cannot get it to work. Here is how I am using it

function module_name_custom_views_pre_execute(&$view) {
    if($view->name=="view_name") {
            if($view->build_info['query_args'][1]==0) {
            $view->build_info['query']="my subquery";

                //echo ($view->build_info['query'] );

Without to test it I tried replacing the $view->build_info['query'] with the query generated in the view. Instead of a view result I get a blank page. Any other query I put in there myself results in a blank page as well.

I am writing the queries like so:

$view->build_info['query']="SELECT nid FROM node WHERE node.status='1'";

So I am not assigning any array objects values but rather writing the query in as if I would in any other db call. Should I build the query through the means of creating an object? So something like this to start:

["build_info"]=> array(3) { 
    ["query"]=> object(SelectQuery)#752 (21) { 
        ["fields":protected]=> array(4) { 
            ["nid"]=> array(3) { 
                ["field"]=> string(3) "nid" 
                ["table"]=> string(4) "node" 
                ["alias"]=> string(3) "nid" } 
        ["node_created"]=> array(3) { 
            ["field"]=> string(7) "created" 
            ["table"]=> string(4) "node" 
            ["alias"]=> string(12) "node_created" } 
        ["og_og_membership_label"]=> array(3) { 
            ["field"]=> string(5) "label" 
            ["table"]=> string(16) "og_og_membership" 
            ["alias"]=> string(22) "og_og_membership_label" } 
        ["node_title"]=> array(3) { 
            ["field"]=> string(5) "title" 
            ["table"]=> string(4) "node" 
            ["alias"]=> string(10) "node_title" } }

Is there is another way to implement a subquery in views?

Using Drupal 7 Views 3

3 Answers 3


Also checkout http://drupal.org/sandbox/Fabianx/1184598 as this is adding a subquery to a view; which is exactly what your trying to do.


There are tons of pre_execute() examples here http://drupal.org/node/409808 that might help.


You will have to make db_select() query object (In Drupal 7 Views 3 you can not use simple SQL query any more). For further detail on "Dynamic queries" go to this link.



Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.