Jump to content

Multiple queries to MySQL


at0mic

Recommended Posts

I have a table of answers to 19 Yes/No questions which people have answered.

 

I am querying each column twice; once to see how many people said 'Yes' to that question, and again to see how many people said 'no' to that question. This means a total of 38 queries.

 

Is this the right way to do it or is there a better way? Like just one BIG query?

 

Here's the code I used to query the first two questions (I left the rest out to keep my post smaller)

 

$q_count = ("SELECT count(intray_id) FROM survey1_results WHERE r1_answer = 'Yes' AND intray_id = ".$HTTP_GET_VARS['id']); 
$r_count = mysql_query($q_count);
if ($r_count)
{
while ( $a = mysql_fetch_row($r_count) )
{
	echo "<tr><td> ".$qrow['q2']." </td><td>" .$a[0] ." People</td>";
}	
}


$q_count = ("SELECT count(intray_id) FROM survey1_results WHERE r1_answer = 'No' AND intray_id = ".$HTTP_GET_VARS['id']); 
$r_count = mysql_query($q_count);
if ($r_count)
{
while ( $a = mysql_fetch_row($r_count) )
{
	echo "<td>" .$a[0] ." People</td></tr>";
}	
}


$q_count = ("SELECT count(intray_id) FROM survey1_results WHERE r2_answer = 'Yes' AND intray_id = ".$HTTP_GET_VARS['id']); 
$r_count = mysql_query($q_count);
if ($r_count)
{
while ( $a = mysql_fetch_row($r_count) )
{
	echo "<tr><td> ".$qrow['q2']." </td><td>" .$a[0] ." People</td>";
}	
}


$q_count = ("SELECT count(intray_id) FROM survey1_results WHERE r2_answer = 'No' AND intray_id = ".$HTTP_GET_VARS['id']); 
$r_count = mysql_query($q_count);
if ($r_count)
{
while ( $a = mysql_fetch_row($r_count) )
{
	echo "<td>" .$a[0] ." People</td></tr>";
}	
}

Link to comment
Share on other sites

The "right way to do it" would have been a table with 19 rows, each with one question instead of a single row with 19.

 

In this case the sql is going to be pretty horrendous with lots of repetition.

 

I'd consider retrieving all the columns in a single query then processing them as an array in php.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.