I need to create a table result (and find out if it IS possible within the constraints of MySQL) that will contain data from 2 tables, but the query itself will be based on 3 tables (including a join-table). And joined results should be added to the output results.
Table Structures:
registrants
id, first name, last name, industryID
industries
id, name
registrants_industries (join table)
id, registrant_id, industry_id
So in the final output results table, any registrant could have 0, 1 or many industries, and those industry names should be tacked onto the end of the final results table.
registrants.id,
registrants.first_name,
registrants.last_name,
industry.name-1,
industry.name-2,
industry.name-3,
industry.name-4,
industry.name-5.
But all columns should align by industry. For example, if there were 5 industries, the output would be like so:
(Industries are in this order:
Painting, Tiling, General Construction, Yard Care, Roofing)
354, Mike, Smith, Painting,’’,’’,Yard Care,’’
599, Joe, Jones, ‘’,’’,’General Construction’,’’,’’
So all industry columns would align and could be sorted in Excel etc.
Is this query output with (My)SQL possible? And if so, an you make any good suggestion on the best approach to this?
I began writing my query only to find no answers that solve my added columns based on matches. And getting it all together by row as I need.
You could use group_concat and get all the industries in a single column. I’m aware that is not an exact answer to your questions, but it comes close and I don’t believe what you ask is possible with only 3 joins.
This will produce