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
LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ess KayEntrapenuerCommented:
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)
0
Kyle HamiltonData ScientistAuthor Commented:
fantastic!

can I select only certain columns from the item table as well?
0
Ess KayEntrapenuerCommented:
yes,  the query i wrote,  i make shortcuts to the tables

iq. items is now just i
and iq.comment is now just c
so in select if you want fields from i and from c you cad do

select i.field1,  i.timefield, i.someotherfield, c.fieldsomething

from.....


or you can use the asterisk  * to select everything

Select * from..

or you can use table.asterisk to select all columns from just one table

select c.*, i.id, i.timestamp
from...


just remember to separe them by commas
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ess KayEntrapenuerCommented:
also if any column has a space you must enclose it in brackets

SELECT i.[my weird fieldname]
from...
1
Kyle HamiltonData ScientistAuthor Commented:
luvly, thank you!
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.