Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP + Mysql: dynamic table name from first query for execution of second query
    primarykey
    data
    text
    <p>I have three tables, the first is a table storing applications, the second is a table storing different online forms (different types of applications), the third is a table that stores actual form data:</p> <pre><code> TABLE applications========= -applicationID (PK) -formID (FK) -formRecordID ==================== TABLE forms========= -formID (PK) -formName -tableName (could be 'form_businessLicense','eventLicense',etc) ==================== TABLE form_businessLicense===== -recordID (PK) -dateSubmitted -(a whole bunch of other data) =============================== </code></pre> <p>"formRecordID" points to "recordID" in "form_businessLicense" or "eventLicense". Since it could reference any table, it can't be a foreign key. So instead I grab the tableName from the "forms" table, then build a query to get all the application data from, say "form_businessLicense".</p> <p>So I need to get data from, say, all applications plus a bit of data from the application form filled out (ex:form_businessLicense). I'm just going to paste my code (I'm actually querying all applications in a given set of IDs):</p> <pre><code>$applications = $this-&gt;selectAll( "SELECT applicationID, formName, tableName, fieldIdentifier, formRecordID, dateSubmitted, DATE_FORMAT(dateSubmitted,'%c/%e/%Y') AS dateSubmittedFormat FROM applications AS a JOIN forms AS f ON a.formID = f.formID WHERE a.applicationID IN (".$applicationIDs.") ORDER BY dateSubmitted ASC" ); for($a=0;$a&lt;count($applications);$a++){ $form = $this-&gt;select("SELECT ".$applications[$a]['fieldIdentifier']." AS identifierName FROM ".$applications[$a]['tableName']." WHERE recordID = ".$applications[$a]['formRecordID'] ); $applications[$a]['identifierName'] = $form['identifierName']; } </code></pre> <p>Is there any way to merge these two queries into one so I don't have to loop over all results and run a separate query for each result? I feel like I could maybe do this with a JOIN but I'm not sure how to reference the "tableName" and "formRecordID" for use in the same SQL statement.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload