Posts | Comments

So I’m working on some code that will allow me to search multiple databases in a single query, returning a resultset.

Note: this is MySQL

  1. – Searching the "clipper" database
  2. – for any image that has ‘dog%’ as a keyword
  3. SELECT
  4.         S.Filename,
  5.         S.Record_ID,
  6.         S.Path,
  7.         S.File_Description
  8. FROM
  9.         clipper.Item_Table S,
  10.         clipper.Keyword C1,
  11.         clipper.Item_Keyword O1
  12. WHERE
  13.         S.Record_ID = O1.Item_ID
  14.     AND
  15.         O1.Keyword_ID = C1.Record_ID
  16.     AND
  17.         C1.Keyword LIKE ‘dog%’
  18. GROUP BY
  19.         S.Record_ID
  20. LIMIT   0,30

What I’m having a tough time with is the clipper.columnName part. Each search may have many, many C[n] and O[n] values (these are the keyword items as they relate to the joins). If I have clipper.THIS and clipper.THAT, then how do I add image_server.THIS and image_server.THAT*?

I’m thinkink I might just loop the queries and pool all the results together into one XML resultset.

There should be a more efficient way, however, right?

* “clipper” and “image_server” are two databases where images are stored.

Could this be the answer?

  1. – Searching the "clipper" AND "image_server"
  2. – databases for any image that has ‘dog%’ as a keyword
  3. SELECT
  4.         S1.Filename,
  5.         S1.Record_ID,
  6.         S1.Path,
  7.         S1.File_Description,
  8.         S2.Filename,
  9.         S2.Record_ID,
  10.         S2.Path,
  11.         S2.File_Description
  12. FROM
  13.         clipper.Item_Table S1,
  14.         clipper.Keyword C1,
  15.         clipper.Item_Keyword O1,
  16.         image_server.Item_Table S2,
  17.         image_server.Keyword C2,
  18.         image_server.Item_Keyword O2
  19. WHERE
  20.         S1.Record_ID = O1.Item_ID
  21.   AND
  22.         S2.Record_ID = O2.Item_ID
  23.   AND
  24.         O1.Keyword_ID = C1.Record_ID
  25.   AND
  26.         O2.Keyword_ID = C2.Record_ID
  27.   AND
  28.         C1.Keyword LIKE ‘dog%’
  29.   AND
  30.         C2.Keyword LIKE ‘dog%’
  31. GROUP BY
  32.         S1.Record_ID,
  33.         S2.Record_ID

Solved:
Thanks to Greg Miley, the problem has been solved:

  1. SELECT
  2. cit.Filename,
  3. cit.Record_ID AS Record_ID,
  4. cit.Path,
  5. cit.File_Description,
  6. "clipper" AS database_name
  7. FROM
  8. clipper.Item_Table cit
  9. JOIN
  10. clipper.Item_Keyword cik
  11.     ON cik.Item_ID = cit.Record_ID
  12. JOIN
  13. clipper.Keyword ck
  14.     ON ck.Record_ID = cik.Keyword_ID
  15. WHERE
  16. ck.Keyword LIKE ‘dog%’
  17. OR
  18. ck.Keyword LIKE ‘cat%’
  19.  
  20. UNION
  21. SELECT
  22. isit.Filename,
  23. isit.Record_ID AS Record_ID,
  24. isit.Path,
  25. isit.File_Description,
  26. "image_server" AS database_name
  27. FROM
  28. image_server.Item_Table isit
  29. JOIN
  30. image_server.Item_Keyword isik
  31.     ON isik.Item_ID = isit.Record_ID
  32. JOIN
  33. image_server.Keyword isk
  34.     ON isk.Record_ID = isik.Keyword_ID
  35. WHERE
  36. isk.Keyword LIKE ‘dog%’
  37. OR
  38. isk.Keyword LIKE ‘cat%’
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Technorati

Leave your comment

chadedge dot-com is powered by WordPress. Design by Nofie Iman.