How to use db API addExpression I am trying to find a way of creating a view of both a custom entity type and a node. I was following this as a guide: https://drupal.org/node/748844 (see the way end for the D7 implementation, I have quoted it below, but for some reason, its not fully displaying...)

I am trying to understand this line

$query2-&gt;addExpression(':the_group_type', 'Type', array(':the_group_type' =&gt; 'Group'));


I see from online documentation that addExpression adds an expression to a list of fields, which I would assume means something like SUM(quantity). But I don't understand what the addExpression lines (there are actually two of them) are doing in this code.

Code listed below:

/**
 * Implements hook_views_pre_execute()
 */
function YOUR_MODULE_NAME_views_pre_execute(&amp;$view){
    if($view-&gt;name == 'YOUR_VIEW_NAME'){
    //extracts my nodes
    $query2 = db_select('node', 'node');
    $query2-&gt;leftJoin('location_instance', 'location_instance', 'node.vid = location_instance.vid');
    $query2-&gt;leftJoin('location', 'location', 'location_instance.lid  = location.lid');

    //created and nid are the same fields used by my_view query
    $query2-&gt;addField('node', 'title', 'users_name');  
    $query2-&gt;addField('node', 'nid', 'uid');  
    $query2-&gt;addField('location', 'latitude', 'location_latitude');  
    $query2-&gt;addField('location', 'longitude', 'location_longitude');  
    $query2-&gt;addExpression(':the_group_type', 'Type', array(':the_group_type' =&gt; 'Group'));

    //set some conditions if necessary
    $query2-&gt;condition('node.type', 'group', '=')
           -&gt;condition('node.status', 1, '=');

    $query1 = $view-&gt;build_info['query'];
    $query1 -&gt;addExpression(':the_user_type', 'Type', array(':the_user_type' =&gt; 'User'));

    //union custom query with my_view default query
    $query2-&gt;union($query1, 'UNION ALL');


    //in order to prevent Cardinality violation errors i have to make a "total" query
    $total_query = db_select($query2,'total')-&gt;fields('total');

    //update views query and count_query with my new query.
    $view-&gt;build_info['query'] = $total_query;
    $view-&gt;build_info['count_query'] = $view-&gt;build_info['query']; //count_query is necessary for the pager
  }
}
Any valid SQL expression is allowed, and that includes placeholders (like in that code), literal values, or SQL functions calls (including aggregate functions, such as COUNT(*)).

$query2-&gt;addExpression(':the_group_type', 'Type', array(':the_group_type' =&gt; 'Group')) is equivalent, in plain SQL, to 'Group' AS Type.
It is an expression that is widely used in the Views module. For example, if you enable the "Add Views signature to all SQL queries" option, the query executed by the Views module will be similar to the following one. (This is the query I get for the front page view.)

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:default' AS view_name
FROM 
{node} node
WHERE (( (node.promote &lt;&gt; '0') AND (node.status = '1') ))
ORDER BY node_sticky DESC, node_created DESC
LIMIT 10 OFFSET 0


The 'frontpage:default' AS view_name part is added using SelectQuery::addExpression() in a way similar to what the code you show does.