MYSQL/php - Procedure question
I am querying a table that has classified ads in it. I need to have find all of the ads that are current (from between now and their expiration date), but all I want is the category that they are in and how many there are from each category. Here is what I have but can't get it to work.
SELECT AdType.AdTypeID,开发者_运维知识库
AdType.AdDescription,
Class_Ads.AdTypeID,
Class_Ads.DateSubmitted
FROM Class_Ads
INNER JOIN AdType ON Class_Ads.AdTypeID = AdType.AdTypeID
WHERE Class_Ads.DateSubmitted BETWEEN NOW()
AND (date_add(Class_Ads.DateSubmitted,INTERVAL 2 MONTH))
GROUP BY AdType.AdDescription
What I want the outcome to look like is:
category1 (5 ads)
category2 (2 ads)
etc.
SELECT AdType.AdDescription, COUNT(*)
FROM Class_ads
INNER JOIN AdType ON Class_Ads.AdTypeID = AdType.AdTypeID
WHERE Class_Ads.DateSubmitted BETWEEN NOW()
AND (date_add(Class_Ads.DateSubmitted,INTERVAL 2 MONTH))
GROUP BY AdType.AdDescription
That should do the trick
精彩评论