Jump to content

SELECT COUNT


WatsonN

Recommended Posts

I'm trying to figure out how to implement SELECT COUNT into a php generated list.

My script lists all the users in one table

$data = mysql_query("SELECT * FROM YBK_Login") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) { 
Print "<tr>"; 
Print "<td>".$info['UID'] . " </td>";
Print "<td>"."#"."</td>";
Print "<td>".$info['ID'] . "</td> "; 
Print "<td>".$info['Allowed'] . "</td> ";
Print "<td>".$info['pass'] . " </td>";
Print "<td>".$info['HR'] . " </td>"; } 

?>

 

And I want to use the count to count the number of entries each user has in a diffrent table.

Like pull the $info['ID'] and check against posts in YBK_Ads with the column name UID

Link to comment
Share on other sites

A subquery should work?

 

$data = mysql_query("SELECT a.*, (SELECT COUNT(posts) FROM YBK_Ads a WHERE a.UID = y.UID) AS postCount FROM YBK_Login y") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) { 



Print "<tr>"; 

Print "<td>".$info['postCount'] . " </td>";

Print "<td>".$info['UID'] . " </td>";



Print "<td>"."#"."</td>";



Print "<td>".$info['ID'] . "</td> "; 



Print "<td>".$info['Allowed'] . "</td> ";



Print "<td>".$info['pass'] . " </td>";



Print "<td>".$info['HR'] . " </td>"; } 

?>

Link to comment
Share on other sites

sorry, mixed up a with y.

 

$data = mysql_query("SELECT y.*, (SELECT COUNT(posts) FROM YBK_Ads a WHERE a.UID = y.UID) AS postCount FROM YBK_Login y") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "<tr>"; Print "<td>".$info['postCount'] . " </td>";Print "<td>".$info['UID'] . " </td>";Print "<td>"."#"."</td>";Print "<td>".$info['ID'] . "</td> "; Print "<td>".$info['Allowed'] . "</td> ";Print "<td>".$info['pass'] . " </td>";Print "<td>".$info['HR'] . " </td>"; } ?>

 

Link to comment
Share on other sites

@eran works perfectly Thanks both of you :)

$data = mysql_query("SELECT y.*,COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID GROUP BY y.ID") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) {  
Print "<tr>"; 
Print "<td>".$info['UID'] . " </td>";
Print "<td>".$info['posts'] . " </td>";
Print "<td>".$info['ID'] . "</td> "; 
Print "<td>".$info['Allowed'] . "</td> ";
Print "<td>".$info['pass'] . " </td>";
Print "<td>".$info['HR'] . " </td>"; } 

 

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.