tqrecords
asked on
Counting a row in sql
Hey,
I have to display a column that is a count of another row on another table. I have done it using two queries but I would like to find out how to do it in one query.
The column to display will be "Number of Exercises" and it will count the number of "Exercise Id's" on the Session Details table.
I have attached my code and the tables to help you all understand how they are related.
Thanks.
I have to display a column that is a count of another row on another table. I have done it using two queries but I would like to find out how to do it in one query.
The column to display will be "Number of Exercises" and it will count the number of "Exercise Id's" on the Session Details table.
I have attached my code and the tables to help you all understand how they are related.
Thanks.
$sql = "Select Sessions.Date_Created, Sessions.Created_By
From Sessions, Session_Details
Where Sessions.Member_Id = '$clientID'";
$result = mysql_query($sql);
while ($ROW = mysql_fetch_array($result)) {
$sessionID = $ROW['Session_Id'];
$sessionDate = $ROW['Date_Created'];
echo date('F d, Y',strtotime($sessionDate));
echo $createdBy = $ROW['Created_By'];
}
// Retrieve the number of exercises in that session
$sql2 = "Select Session_Details.Exercise_Id
From Session_Details, Sessions
Where Sessions.Member_Id = '$clientID' AND Session_Details.Session_Id = Sessions.Session_Id";
$result2 = mysql_query($sql2);
echo mysql_num_rows($result2);
tables.bmp
ASKER
Thanks, but there is also one more join I need to make from another table.
There is a created_by column in the Sessions table and it is an integer value.
The created_by will match the member_id on the members table and will return the name of the member.
I have the updated tables attached.
tables.bmp
There is a created_by column in the Sessions table and it is an integer value.
The created_by will match the member_id on the members table and will return the name of the member.
I have the updated tables attached.
tables.bmp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window