Solved

Counting a row in sql

Posted on 2009-03-30
3
274 Views
Last Modified: 2012-05-06
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.
$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);

Open in new window

tables.bmp
0
Comment
Question by:tqrecords
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24023920
you mean:
Select s.Date_Created, s.Created_By, count(sd.exercice_id) exercice_count
From Sessions s 
left join Session_Details sd
  on sd.session_id = s.session_id
Where s.Member_Id = '$clientID'
group by s.Member_id

Open in new window

0
 

Author Comment

by:tqrecords
ID: 24024294
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24026796
simply add a join
Select s.Date_Created, s.Created_By, count(sd.exercice_id) exercice_count
 , m.username created_by_user
From Sessions s 
left join Session_Details sd
  on sd.session_id = s.session_id
join members m
  on m.member_id = s.created_by
Where s.Member_Id = '$clientID'
group by s.Member_id

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question