If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Best query||design for table>subtable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-09-06, 11:02
orxata orxata is offline
Registered User
 
Join Date: Feb 2006
Posts: 2
Best query||design for table>subtable

Hi,

I've had few times the same problem and i'd like to go over it.
I'm facing this kind of data:

say that i need to store documents, all of them have a set of fields in common and all also have specific fields:

articles [ TITLE, AUTHOR, DATE, PIC, EXTRACT, KEYWORDS, ... ]
news [ TITLE, AUTHOR, DATE, URL, SOURCE, KEYWORDS, ... ]
...

my first solution was to store all of this data like this:
doc [ TITLE, AUTHOR, DATE, KEYWORDS, TABLENAME, TABLEID ]
doc_articles [ ID, PIC, EXTRACT ]
doc_news [ID, URL, SOURCE ]

It works fine BUT i need 2 queries for each doc to get its corresponding specific data.

Is there a way to dynamicaly link the corresponding fields leading to a result set with all table fields??

Is there another way to design tables to make it easier?

Thx in advance for reading,

++
Reply With Quote
  #2 (permalink)  
Old 02-09-06, 11:27
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Check to see how your database engine supports the JOIN operation. I think it will do what you want.

-PatP
Reply With Quote
  #3 (permalink)  
Old 02-09-06, 11:56
orxata orxata is offline
Registered User
 
Join Date: Feb 2006
Posts: 2
Yes, i may not have been precise enough,

I actually JOIN when i know TABLENAME:

If i want news rows i do a

select * from doc,doc_news left join doc.TABLEID=doc_news.ID where doc.TABLENAME=doc_news


BUT i sometimes get a doc row without knowing if its a news or an article.... i then need a 2nd query to get the specific table rows.

Would there be a query to select the doc row AND the specific table row with one query?

I'd say, Is there dynamic SQL possilities like

SELECT * from doc,doc.TABLENAME where doc.ID=X ??
(doesn't work "as is" in mysql)...

++
Reply With Quote
  #4 (permalink)  
Old 02-09-06, 12:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
use LEFT OUTER JOINs to join the doc table to both the news and the articles table

each PK in doc will have only one matching FK in either news or articles, so bob's your uncle -- still only one query!!



but check your syntax carefully, because this isn't even close to being valid syntax --

select * from doc
,doc_news
left join doc.TABLEID=doc_news.ID
where doc.TABLENAME=doc_news
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-09-06, 13:35
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
if you must continue with your current design you could also consider aliasing
eg
select Article.docID as ArtDoc, Article.Author as ArtAuthor from blah

not nice but it may help
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On