Hello :)

Despite I'm using Drupal for a couple of years now, I'm still pretty new to module development. The search was not very helpful, so I post my question in the hope someone can help me with this:

I ran into a problem where I have to JOIN from Drupal's database and also from a second one.
My first quickfix solution to this problem was to use variable_get() and let the user assign the name of the second database. Since I dont think this is very comfortable I would prefer using multiple database connections with the db_set_active() function.

I think the problem is clear: db_set_active() rewrites the curly brackets to the active database and is not able to use both at the same time (ok how should it know which one is meant ;-) ).

For example:

$query="SELECT DISTINCT $d.{profile_values}.uid AS uid
  FROM ". $b .".users
  INNER JOIN $d.{profile_values} ON $b.users.nickname = $d.{profile_values}.value
  JOIN $d.{users} ON $d.{profile_values}.uid = $d.{users}.uid
  WHERE $d.{profile_values}.uid NOT IN (SELECT DISTINCT $d.{profile_values}.uid AS uid FROM $b.users
  INNER JOIN $d.{profile_values} ON $b.users.nickname = $d.{profile_values}.value
  JOIN $d.{users} ON $d.{profile_values}.uid = $d.{users}.uid
  JOIN $d.{users_roles} ON $d.{users}.uid = $d.{users_roles}.uid
  WHERE $d.{users_roles}.rid = ". $rid .");";

Hopefully someone can give me directions to solve this issue elegantly.
Thank you in advance
sincerely
Hannes

Comments

yfreeman’s picture

you would have to query each databases separately and then with you own code aggregate the data and return the results you want

Sree’s picture

get the query results separately & write some custom script to perform this.

-- Sree --
IRC Nick: sreeveturi