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'));