SQL related tables
5 pts.
0
Q:
SQL related tables
I have 2 tables one with all my products and their accociated details and another with comments on the products.

Product table 1
prodID, title, description, price

Comments table 2
commID, prodID, comment

I would like to export this to a single XML file, thus an XML element for each product from table 1 with all its comments in table 2 as child nodes within the product emelement:

<prods>
<prod>
<title></title>
<description></description>
<price></price>
<comments>
<comment></comment>
<comment></comment>
<comment></comment>
</comments>
</prod>
</prods>

I've tried a JOIN statement, but that returns a product row for each comment.

What is the correct SQL SELECT to return one product row with all its comment rows? Thanks...

 

Software/Hardware used:
MySQL5+, PHP5+
ASKED: Oct 16 2009  7:10 AM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
1840 pts.
0
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • AddThis Social Bookmark Button
I do not know about putting in the XML tags - I know some version of DB2 will do it.

But ignoring the XML, I see two different ways to get the data.

1) are you in a program? if so, open a cursor against product table and for every row open a cursor against co0mments table and process the rows accordingly.

2) are you trying to do this in one SQL statement? then something like this will return your data columns (but again I am skipping the XML tags)
SELECT T1.PRODID, T1.DESCRIPTION, 0
FROM PRODUCT TABLE T1
UNION
SELECT T2.PRODID, T2.COMMENT, T2.COMMID
FROM COMMENTTABLE T2
ORDER BY 1,3
Last Answered: Oct 19 2009  8:26 PM GMT by Meandyou   1840 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



0