1

How can I write the following query in a Drupal-friendly way?

select max(cnt) from 
(select COUNT(*) as cnt from Det a,
dbo.Mem b where a.no=b.no
group by b.no) X
go
0

1 Answer 1

3

Since you're going cross-database I think using db_select() could get quite complicated. I might be wrong though, it might just work.

Either way, you could always just run the query directly with db_query():

$sql = '
  select max(cnt) from 
  (select COUNT(*) as cnt from Det a,
  dbo.Mem b where a.no=b.no
  group by b.no)';

$count = db_query($sql)->fetchField();
2
  • 1
    I agree with this one. Furthermore, be sure that you don't use {} brackets around tables names in the "foreign" database as Drupal will attempt to add table prefixes.
    – sbrattla
    Sep 5, 2012 at 13:12
  • @sbrattla Very good point, didn't think of that
    – Clive
    Sep 5, 2012 at 13:16

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.