Skip to main content

D7 SelectQuery, db_query() and pager

I have a long query with sub-queries using db_query()db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefaultPagerDefault (replaces pager_query() of D6Drupal 6 pager_query()).

I need my query with pager. Can I use Extenders PagerDefaultPagerDefault with db_query db_query()?
If I have to use db_select()db_select() for pager, how can I implement my query with sub-queries using db_select() db_select()?

$result = db_query("SELECT leg.*,
    ( SELECT COUNT(*)
    FROM {field_data_field_leg}
    WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews
FROM 
    ( SELECT
        n.nid, n.created, title, field_from_value origin, field_to_value destination,
        field_price_value price, term.name transport
    FROM {node} n
        JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
        JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
        JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
        JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
        JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
        JOIN {field_data_field_transport_type} ttype
            ON t.field_transport_nid = ttype.entity_id
        JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
    WHERE n.type = :type
    ORDER BY n.created DESC, field_price_value ASC
) AS leg
GROUP BY nid 
ORDER BY created DESC",
array(':bundle'=>'review', ':type' => 'leg'));

D7 SelectQuery, db_query and pager

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces pager_query() of D6).

I need my query with pager. Can I use Extenders PagerDefault with db_query ?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select() ?

$result = db_query("SELECT leg.*,
    ( SELECT COUNT(*)
    FROM {field_data_field_leg}
    WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews
FROM 
    ( SELECT
        n.nid, n.created, title, field_from_value origin, field_to_value destination,
        field_price_value price, term.name transport
    FROM {node} n
        JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
        JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
        JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
        JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
        JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
        JOIN {field_data_field_transport_type} ttype
            ON t.field_transport_nid = ttype.entity_id
        JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
    WHERE n.type = :type
    ORDER BY n.created DESC, field_price_value ASC
) AS leg
GROUP BY nid 
ORDER BY created DESC",
array(':bundle'=>'review', ':type' => 'leg'));

SelectQuery, db_query() and pager

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces Drupal 6 pager_query()).

I need my query with pager. Can I use PagerDefault with db_query()?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select()?

$result = db_query("SELECT leg.*,
    ( SELECT COUNT(*)
    FROM {field_data_field_leg}
    WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews
FROM 
    ( SELECT
        n.nid, n.created, title, field_from_value origin, field_to_value destination,
        field_price_value price, term.name transport
    FROM {node} n
        JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
        JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
        JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
        JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
        JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
        JOIN {field_data_field_transport_type} ttype
            ON t.field_transport_nid = ttype.entity_id
        JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
    WHERE n.type = :type
    ORDER BY n.created DESC, field_price_value ASC
) AS leg
GROUP BY nid 
ORDER BY created DESC",
array(':bundle'=>'review', ':type' => 'leg'));

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces pager_query() of D6).

I need my query with pager. Can I use Extenders PagerDefault with db_query ?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select() ?

$result = db_query("SELECT leg.*,
    ( SELECT COUNT(*) 
 from   FROM {field_data_field_leg}
    WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews  
FROM 
       ( SELECT
    (   SELECT n.nid, n.created, title, field_from_value origin, field_to_value destination, field_price_value price, term.name transport
         field_price_value price, term.name transport
    FROM {node} n 
                JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
                JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
                JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
                JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
                JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
        JOIN {field_data_field_transport_type} ttype
         JOIN {field_data_field_transport_type} ttype ON t.field_transport_nid = ttype.entity_id
                JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
                WHERE n.type = :type
                ORDER BY n.created DESC, field_price_value ASC
            ) AS leg  
GROUP BY nid 
            ORDER BY created DESC",  
array(':bundle'=>'review', ':type' => 'leg'));

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces pager_query() of D6).

I need my query with pager. Can I use Extenders PagerDefault with db_query ?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select() ?

$result = db_query("SELECT leg.*, ( SELECT COUNT(*) from {field_data_field_leg} WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews FROM 
            (   SELECT n.nid, n.created, title, field_from_value origin, field_to_value destination, field_price_value price, term.name transport
                FROM {node} n 
                JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
                JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
                JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
                JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
                JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
                JOIN {field_data_field_transport_type} ttype ON t.field_transport_nid = ttype.entity_id
                JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
                WHERE n.type = :type
                ORDER BY n.created DESC, field_price_value ASC
            ) AS leg GROUP BY nid 
            ORDER BY created DESC", array(':bundle'=>'review', ':type' => 'leg'));

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces pager_query() of D6).

I need my query with pager. Can I use Extenders PagerDefault with db_query ?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select() ?

$result = db_query("SELECT leg.*,
    ( SELECT COUNT(*) 
    FROM {field_data_field_leg}
    WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews 
FROM 
    ( SELECT
        n.nid, n.created, title, field_from_value origin, field_to_value destination,
        field_price_value price, term.name transport
    FROM {node} n
        JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
        JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
        JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
        JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
        JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
        JOIN {field_data_field_transport_type} ttype
            ON t.field_transport_nid = ttype.entity_id
        JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
    WHERE n.type = :type
    ORDER BY n.created DESC, field_price_value ASC
) AS leg 
GROUP BY nid 
ORDER BY created DESC", 
array(':bundle'=>'review', ':type' => 'leg'));
Source Link
Sithu
  • 5k
  • 2
  • 35
  • 55

D7 SelectQuery, db_query and pager

I have a long query with sub-queries using db_query().
So far I know SelectQuery of D7 comes with Extenders. Example extenders are PagerDefault (replaces pager_query() of D6).

I need my query with pager. Can I use Extenders PagerDefault with db_query ?
If I have to use db_select() for pager, how can I implement my query with sub-queries using db_select() ?

$result = db_query("SELECT leg.*, ( SELECT COUNT(*) from {field_data_field_leg} WHERE field_leg_nid = leg.nid AND bundle = :bundle ) AS reviews FROM 
            (   SELECT n.nid, n.created, title, field_from_value origin, field_to_value destination, field_price_value price, term.name transport
                FROM {node} n 
                JOIN {field_data_field_leg} l ON n.nid = l.field_leg_nid
                JOIN {field_data_field_from} ff ON n.nid = ff.entity_id
                JOIN {field_data_field_to} ft ON n.nid = ft.entity_id
                JOIN {field_data_field_transport} t ON l.entity_id = t.field_transport_nid
                JOIN {field_data_field_price} p ON t.entity_id = p.entity_id
                JOIN {field_data_field_transport_type} ttype ON t.field_transport_nid = ttype.entity_id
                JOIN {taxonomy_term_data term} ON ttype.field_transport_type_tid = term.tid
                WHERE n.type = :type
                ORDER BY n.created DESC, field_price_value ASC
            ) AS leg GROUP BY nid 
            ORDER BY created DESC", array(':bundle'=>'review', ':type' => 'leg'));