SELECT SUM不按要求限制

时间:2014-08-22 05:31:51

标签: mysql sql database

我对数据库之地完全陌生。我想从我的数据库中获取多个列的总数,所以我正在这样做...(下面)。问题是,当它返回每列的总数时,它不会将其限制为最后7行的日期。它正在返回该顾问的总数。

$result = mysql_query("SELECT SUM(NewPermJobs) AS NewPermJobsTotal, 
SUM(CandidatesSubmitted) AS CandidatesSubmittedTotal, SUM(FirstInterviewsRecorded) 
AS FirstInterviewsRecordedTotal, SUM(OldJobsReactivated) AS OldJobsReactivatedTotal, 
SUM(CandidateRecordsUpdated) AS CandidateRecordsUpdatedTotal, SUM(CompaniesAddedDream) 
AS CompaniesAddedDreamTotal, SUM(SocialContentShared) AS SocialContentSharedTotal, 
SUM(ApplicantStatusChanged) AS ApplicantStatusChangedTotal, SUM(JobsClosed) AS 
JobsClosedTotal, SUM(Revenue) AS RevenueTotal FROM dailyactivity WHERE (`Consultant` 
LIKE '%".$query."%') ORDER BY Date DESC LIMIT 0,7");?>

它可能非常混乱但是我能在这个阶段提出的最好的。知道为什么它没有将列的总和限制为按日期desc排序的最后7列吗?

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

您可以先使用子查询限制结果,然后执行总和,如下所示:

SELECT SUM(NewPermJobs) AS NewPermJobsTotal, 
  SUM(CandidatesSubmitted) AS CandidatesSubmittedTotal, 
  SUM(FirstInterviewsRecorded) AS FirstInterviewsRecordedTotal, 
  SUM(OldJobsReactivated) AS OldJobsReactivatedTotal, 
  SUM(CandidateRecordsUpdated) AS CandidateRecordsUpdatedTotal, 
  SUM(CompaniesAddedDream) AS CompaniesAddedDreamTotal, 
  SUM(SocialContentShared) AS SocialContentSharedTotal, 
  SUM(ApplicantStatusChanged) AS ApplicantStatusChangedTotal, 
  SUM(JobsClosed) AS JobsClosedTotal, 
  SUM(Revenue) AS RevenueTotal 
FROM (
   SELECT NewPermJobs, CandidatesSubmitted, FirstInterviewsRecorded, 
          OldJobsReactivated, CandidateRecordsUpdated, CompaniesAddedDream, 
          SocialContentShared, ApplicantStatusChanged, JobsClosed, Revenue
   FROM dailyactivity 
   WHERE (`Consultant` LIKE '%".$query."%') 
   ORDER BY Date DESC 
   LIMIT 0,7
) t

答案 1 :(得分:-1)

$result = mysql_query("SELECT SUM(NewPermJobs) AS NewPermJobsTotal, 
SUM(CandidatesSubmitted) AS CandidatesSubmittedTotal, SUM(FirstInterviewsRecorded) 
AS FirstInterviewsRecordedTotal, SUM(OldJobsReactivated) AS OldJobsReactivatedTotal, 
SUM(CandidateRecordsUpdated) AS CandidateRecordsUpdatedTotal, SUM(CompaniesAddedDream) 
 AS CompaniesAddedDreamTotal, SUM(SocialContentShared) AS SocialContentSharedTotal, 
SUM(ApplicantStatusChanged) AS ApplicantStatusChangedTotal, SUM(JobsClosed) AS 
JobsClosedTotal, SUM(Revenue) AS RevenueTotal FROM dailyactivity WHERE (`Consultant` 
LIKE '%".$query."%') group by 'required_column_name' ORDER BY Date DESC LIMIT 0,7");?>

使用分组......它将解决您的问题......