• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Is it possible to select across multiple rows into the same result?

Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I will need to be querying a database schema which models an object hierarchy tree, with a single table containing every parent-child relationship in the tree. So to find the TypeA children of a specific parent you'd do something like

SELECT * FROM TypeA, relationships WHERE relationships.parentid = <parentid> AND relationships.childid = TypeA.id

That's straightforward enough, but what if you're trying to find grandchildren? Or a single query involving multiple different types (tables) of children? That would mean that multiple rows of "relationships.childid" would be used for the same row in the result set. Is there any way to do this without using either multiple seperate queries or nested queries.

Changing the table design of the schema is not an option, unfortunately, though it is possible to add new tables and views.

Thank you,
author & internet detective
Posts: 40200
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the "union" operator to separate different numbers of joins. While the database still has to execute the parts of the query, it is only one database round trip. This only works if you know how many levels you can have though. It may be more efficient to do the separate queries.
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, thank you!

Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic