Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem: Writing a MySQL parser to split JOIN's and run them as individual queries (denormalizing the query dynamically)
    text
    copied!<p>I am trying to figure out a script to take a MySQL query and turn it into individual queries, i.e. denormalizing the query dynamically.</p> <p>As a test I have built a simple article system that has 4 tables:</p> <ul> <li>articles <ul> <li>article_id</li> <li>article_format_id</li> <li>article_title</li> <li>article_body</li> <li>article_date</li> </ul></li> <li>article_categories <ul> <li>article_id</li> <li>category_id</li> </ul></li> <li>categories <ul> <li>category_id</li> <li>category_title</li> </ul></li> <li>formats <ul> <li>format_id</li> <li>format_title</li> </ul></li> </ul> <p>An article can be in more than one category but only have one format. I feel this is a good example of a real-life situation.</p> <p>On the category page which lists all of the articles (pulling in the format_title as well) this could be easily achieved with the following query:</p> <pre><code>SELECT articles.*, formats.format_title FROM articles INNER JOIN formats ON articles.article_format_id = formats.format_id INNER JOIN article_categories ON articles.article_id = article_categories.article_id WHERE article_categories.category_id = 2 ORDER BY articles.article_date DESC </code></pre> <p>However the script I am trying to build would receive this query, parse it and run the queries individually.</p> <p>So in this category page example the script would effectively run this (worked out dynamically):</p> <pre><code>// Select article_categories $sql = "SELECT * FROM article_categories WHERE category_id = 2"; $query = mysql_query($sql); while ($row_article_categories = mysql_fetch_array($query, MYSQL_ASSOC)) { // Select articles $sql2 = "SELECT * FROM articles WHERE article_id = " . $row_article_categories['article_id']; $query2 = mysql_query($sql2); while ($row_articles = mysql_fetch_array($query2, MYSQL_ASSOC)) { // Select formats $sql3 = "SELECT * FROM formats WHERE format_id = " . $row_articles['article_format_id']; $query3 = mysql_query($sql3); $row_formats = mysql_fetch_array($query3, MYSQL_ASSOC); // Merge articles and formats $row_articles = array_merge($row_articles, $row_formats); // Add to array $out[] = $row_articles; } } // Sort articles by date foreach ($out as $key =&gt; $row) { $arr[$key] = $row['article_date']; } array_multisort($arr, SORT_DESC, $out); // Output articles - this would not be part of the script obviously it should just return the $out array foreach ($out as $row) { echo '&lt;p&gt;&lt;a href="article.php?id='.$row['article_id'].'"&gt;'.$row['article_title'].'&lt;/a&gt; &lt;i&gt;('.$row['format_title'].')&lt;/i&gt;&lt;br /&gt;'.$row['article_body'].'&lt;br /&gt;&lt;span class="date"&gt;'.date("F jS Y", strtotime($row['article_date'])).'&lt;/span&gt;&lt;/p&gt;'; } </code></pre> <p>The challenges of this are working out the correct queries in the right order, as you can put column names for SELECT and JOIN's in any order in the query (this is what MySQL and other SQL databases translate so well) and working out the information logic in PHP.</p> <p>I am currently parsing the query using <a href="http://pear.php.net/package/SQL_Parser" rel="nofollow">SQL_Parser</a> which works well in splitting up the query into a multi-dimensional array, but working out the stuff mentioned above is the headache.</p> <p>Any help or suggestions would be much appreciated.</p>
 

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