Avatar of Kyle Hamilton
Kyle Hamilton
Flag for United States of America asked on

MYSQL query syntax

I'd like to export the combined results of two queries to CSV. The export to csv is not an issue.

I need help writing the query which will give me all the results I need. I'm using MySQLWorkbench.

I have some rows from this query:

SELECT * FROM iq.item where grouping_id='334' && class='D';

Open in new window


Then under each result, I would like to list the results from another query:

SELECT * FROM iq.comment where item_id = 2294;

Open in new window


where 'item_id' is the 'id' from the first query. there are multiple rows for each row in the first query.

I'm guessing these have to be joined?

Both tables have a date_created column, which I don't want to duplicate, as well as a content column, which is named differently. One is called 'description' and the second is called 'content'. I would like these two to become one column.

Please let me know if I need to clarify anything else.
Thanks in advance,
Kyle
DatabasesSQL

Avatar of undefined
Last Comment
Kyle Hamilton

8/22/2022 - Mon
Ess Kay

SELECT * 
FROM iq.item i LEFT JOIN
  iq.comment c ON i.id = c.item_id

where i.grouping_id='334' && i.class='D';

Open in new window



if you want all columns from item and specific columns from comments

SELECT i.*, c.item_id, c.columnb, c.columnc, c.col......(insert comuns here)
Kyle Hamilton

ASKER
fantastic!

can I select only certain columns from the item table as well?
ASKER CERTIFIED SOLUTION
Ess Kay

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ess Kay

also if any column has a space you must enclose it in brackets

SELECT i.[my weird fieldname]
from...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Kyle Hamilton

ASKER
luvly, thank you!
:)