Jump to content

Query to tally up the count?


Backstab

Recommended Posts

Say I have two tables, assignments and students.

Each have a auto_incement key value.

 

How would make a query which would return a table containing two columns, one with the count of the students, and one with the count of the assignments?

 

students(student_id)

assignent(assignment_id)

 

Say there were 4 students and 5 assigments.

 

select count(student_id), count(assignment_id) from students, assignments

returns a table

count(student_id) | count(assigment_id)
         20      |          20

 

rather than the

count(student_id) | count(assigment_id)
         4      |          5

that I expected.

 

My problem is combining the data into one table with out them multiplying together.

 

 

Server version: 5.0.75-0ubuntu10

Link to comment
https://forums.phpfreaks.com/topic/157141-query-to-tally-up-the-count/
Share on other sites

Uh... so why do you want 2 columns if they're going to be the same value anyways?

 

I want a table which holds the counts of the number of students and the number of assignments.

 

select count(student_id) from students;  = 4

 

select count(assigment_id) from assignments;  = 5

 

I'm need help with a query that makes a table displaying student count and assignment count.

 

the query I posted before isn't right because it multiplies the counts together, rather just displaying them.

 

 

 

It doesn't make sense to have it in 2 columns when you have different number of rows per column in those 2 tables.

 

You can do this:

SELECT COUNT(student_id) FROM students UNION SELECT COUNT(assignment_id) FROM assignments;

That would give you 2 rows. The first is student_id and the second is assignment_id.

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.