title: Values from one table as fieldnames on another table - complex query
body: <p>I'm trying to do a query that is joining two tables. This join is for a CSV output and requires the values of one table to be the column headings of the CSV with the actual value as values showing 1 or 0.</p>
  
  <p>I can do this by looping through the first table with PHP and doing queries with each iteration of the loop.. but that will be way too hard on the server since we're talking thousands of records. I'm hoping it would be possible with a single query.</p>
  
  <p>As an example, we have two tables. tblSchedule and tblResults</p>
  
  <p>tblSchedule has the following columns..</p>
  
  <pre><code>SID (unique)
  NameVal (name of the speaker varchar(250))
  </code></pre>
  
  <p>tblResults has the following columns..</p>
  
  <pre><code>RID (unique)
  SID (foreign key)
  FirstName (name of participant varchar(250)))
  LastName (name of participant varchar(250)))
  </code></pre>
  
  <p>Before, all I needed to provide for a CSV output was tblResults and joining tblSchedule.. So participant and speaker name were output.</p>
  
  <p>NOW.. which sucks, is that the want a CSV output with column names for EVERY speaker. The records are items in tblResults, and if that particular participant is seeing that speaker in tblSchedule, to show a 1 in that column, else shows 0.</p>
  
  <p>I have 160 entries in tblSchedule, so that would be 160 columns, and I have about 14,000 entries in tblResults.</p>
  
  <p>I maybe overthinking this a bit.</p>
  
  <p>So the CSV would look something like this...</p>
  
  <pre><code>FirstName, LastName, Speaker1, Speaker2, Speaker3,..... Speaker160
  Joe, Schmoe, 0, 0, 1,.....0
  </code></pre>