Jump to content

Using a subQuery to Count


timcadieux

Recommended Posts

I'm using the below query to count the number of Names attached to each particular Unit, but when I ran this Query, WAMP almost died and I lost control of my PC for about 5min until I stopped  ALL Services.

 

Ideas?

		mysql_connect($db_host,$MySqlUN,$MySqlPW);

		@mysql_select_db($database) or die( "Unable to select databases");

		// Build SQL Query  
		$query = "SELECT ID, fldUnit, 
					(SELECT COUNT(*) FROM tblnames as NUMS WHERE tblUnit_ID = Units.ID) as myCount
				from tblunit AS Units";
		$result = mysql_query($query) or die($query);


			// fldunit	
			echo "<tr valign='middle'>\n";
			echo "<tr>\n";
			echo "<td style='background-color: #fcfaf6;'>Field Unit: ";
			echo "</td>\n";
			echo "<td><select name='lstUnits' id='lstUnits'>\n";

				// now you can display the results returned
				  while ($row= mysql_fetch_array($result)) {

					echo "<option value='".$row[0]."'";
					echo ">".$row[1]." ".$NUMS."-Records</option>\n";

				  }
				   
				//Close the db connection
				mysql_close();
				$query='';

			echo "</select></td>\n";
			echo "</tr>\n";			


 

Link to comment
https://forums.phpfreaks.com/topic/152444-using-a-subquery-to-count/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.