Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this query be done without a loop?
    primarykey
    data
    text
    <p>So, basically, I have a MySQL table called 'topics' and another one called 'replies', for example. In table 'topics', there's a field called 'relforum' which relates this topic to a forum section. And in the table 'replies', there's a field called 'reltopic', which relates the reply to a topic. Both tables have an id field, auto_increment primary key.</p> <p>Now, I want to select all replies from a certain forum. Since 'replies' has no 'relforum' field, my way would be:</p> <ul> <li>Select all topics with 'relforum' equal to that certain forum and loop through them</li> <li>While in the loop, select all replies from the topic that is being 'processed' right now</li> <li>Merge all fetch_array results in one multidimensional array, then loop through them.</li> </ul> <p>That's something like this:</p> <pre><code>$query = mysql_query("SELECT * FROM `topics` WHERE `relforum` = '1'"); while($array = mysql_fetch_array($query)) { $temp = mysql_query("SELECT * FROM `replies` WHERE `reltopic` = {$array['id']}"); $results[] = mysql_fetch_array($temp); } </code></pre> <p>Is there a way to merge all that into fewer queries? Because this process would basically run one query per topic in that forum plus one. That would be too much :P</p> <p>Adding the relforum field to the replies table is a solution (I'm still designing the DB Part so it's not a problem to add it), but I would like to see if there's a solution.</p> <p>I'm really not good at SQL things, I only know the basic SELECT/INSERT/UPDATE, and I usually generate the last two ones using PHPMyAdmin, so... I guess I need some help.</p> <p>Thanks for reading!</p>
    singulars
    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.
    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