0

Hey guys I have a mysql table called interests with 4 columns. interestID, name, categoryID interest_desc and date. the categoryID column is linked to a seperate table. How would I use a mysql query that checked how many interests are in a certain category?

Im guessing i use some sort of count() query?

Thanks guys

Update -

$count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interests GROUP by categoryID; ";     $answer = mysql_query($count_query_v1) or die(mysql_error()); echo $answer;

Getting closer but still not perfect, i want to echo out the categoryID with the most interestID's

4 Answers 4

2
select category_name, count(*) as total
from interests i left join category c on c.category_id = i.category_id
group by i.category_id;
Sign up to request clarification or add additional context in comments.

Comments

0

count + group by,
assuming interestID is the unique primary key,
and each interest is tied to single category (as what you have shown)

select categoryID, count(*) as total
from interests
group by categoryID;

// the above example is a simple group by ID without using inner join

output :-

categoryID, total

1 Comment

Trying to print out the categoryID and the total interests $count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interests GROUP by categoryID; "; $answer = mysql_query($count_query_v1) or die(mysql_error()); echo $answer;
0

SELECT COUNT(interestID) FROM interests WHERE categoryID = 'yourvalue';

SELECT COUNT(interestID), categoryID FROM interests GROUP BY categoryID;

4 Comments

how would i go about echoing the category with the most interestsID's
You want to echo : the number,the name of the category, with categoryID belonging to another table let's say category?
I want to get the category with the most interestID's in it
SELECT MAX(COUNT(interestID)), categoryID FROM interests GROUP BY categoryID;
-1

Since you are using the insert query each query will insert one record, so just count the number of insert queries you run by using a counter varialble.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.