Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ParisLiakos on
this is the full query
SELECT t.*
FROM
table AS t
JOIN
( SELECT title, MAX(last_sent) AS maxts
FROM table
GROUP BY title
) AS grp
ON grp.title = t.title
AND grp.maxts = t.last_sent
LEFT OUTER JOIN table_l list ON t.title = list.title
I try to convert it to db_select since i need to use pager and order by on my table.
i want to do something like this:
$query = db_select('table', 't')
->extend('PagerDefault')
->extend('TableSort');
$query->fields('t');
$query->join('
SELECT title, MAX(last_sent) AS maxts
FROM table
GROUP BY title',
'grp', 'grp.title = t.title AND grp.maxts = t.last_sent');
$query->leftjoin('table_l', 'list', 't.title = list.title');
$query->limit(50);
$query->orderByHeader($header);
$result = $query->execute();
but this of course doesn't work since join needs a table name as first argument..
does db_select even support something like this,or do i need to do it manually?
Comments
db_query
i ended up using db_query, since db_select doesn't support that complex queries..
i lost paging and sorting abilities though :(
would be glad if someone had a solution
pager_query implementation for D7
I have a lot of quite complex queries (with subqueries etc) that I just couldn't seem to get the query builder to output the correct SQL for. And I needed paging. So, I reimplemented pager_query() for D7. Only lightly tested, but it seems to work.
create another query object
I am pretty sure I have done this in the past, and from memory you have to create another query object.
Can't find my code right now so I am working from memory here, and I'm pretty sure I have seen an example somewhere.....
I believe it works someting like this:
more info...
I was on the right track. If you look at a file called database_test.test in your Drupal installation (part of the simpletest module), you will find this:
Also noticed in the documentation somewhere that even on the db_select you do not need to specify a table, you can specify another query object.
Hope this helps. Things like this are extremely difficult to unearth...
thank you!!! that indeed
thank you!!!
that indeed works:)))
btw i have send you an email through drupal's contact form about your module.did you ever get it?