SQL related tables

5 pts.
Tags:
MySQL
MySQL 5.0
PHP
SQL Related Tables
SQL 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+

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following