Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a join with three tables when a field might be null
    primarykey
    data
    text
    <p>The code below works great. It combines data from two MySQL tables. I would like to modify it by pulling in some data from a third MySQL table called <code>comment</code>. </p> <p>In the HTML table below, <code>title</code> is a field in the MySQL table <code>submission</code>. Every <code>title</code> has a corresponding <code>submissionid</code> field. The field <code>submissionid</code> is also found in the <code>comment</code> MySQL table. </p> <p>In the HTML table below, I would like <code>countComments</code> to equal the number of times a field called <code>commentid</code> appears in the MySQL table <code>comment</code> for any given <code>submissionid</code>,where the <code>submissionid</code> is the same in both the <code>submission</code> and <code>comment</code> tables, and where the <code>submissionid</code> corresponds to the <code>title</code> being used.</p> <p>Here's the catch: if there is no row in the MySQL table <code>comment</code> that corresponds with the <code>submissionid</code> being used for <code>table</code>, I would like <code>countComments</code> to equal to zero.</p> <p>How can I do this?</p> <pre><code>$sqlStr = "SELECT s.loginid, s.title, s.url, s.displayurl, l.username FROM submission AS s, login AS l WHERE s.loginid = l.loginid ORDER BY s.datesubmitted DESC LIMIT 10"; $result = mysql_query($sqlStr); $arr = array(); echo "&lt;table class=\"samplesrec\"&gt;"; while ($row = mysql_fetch_array($result)) { echo '&lt;tr&gt;'; echo '&lt;td class="sitename1"&gt;&lt;a href="http://www.'.$row["url"].'"&gt;'.$row["title"].'&lt;/a&gt;&lt;/td&gt;'; echo '&lt;/tr&gt;'; echo '&lt;tr&gt;'; echo '&lt;td class="sitename2"&gt;&lt;a href="http://www...com/sandbox/members/index.php?profile='.$row["username"].'"&gt;'.$row["username"].'&lt;/a&gt;&lt;a href="http://www...com/sandbox/comments/index.php?submission='.$row["title"].'"&gt;'.$row["countComments"].'&lt;/a&gt;&lt;/td&gt;'; echo '&lt;/tr&gt;'; } echo "&lt;/table&gt;"; </code></pre>
    singulars
    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