我有一个数据库,用户可以在其中提交有关他们采取了多少步骤的信息,我试图将其拉出来制作排行榜,但由于它是单独提交的,因此他们会单独提取。
SELECT step_count.steps, step_count.email, logins.FirstName, logins.LastName
FROM step_count, logins
WHERE step_count.email=logins.email
这是查询,这就是提取信息的方式
steps | email | FirstName | LastName
5000 | one@test.com | One | onetest
300 | two@test.com | Two | twotest
200 | one@test.com | One | onetest
有没有办法将两者合并在一起以获得总步数?
答案 0 :(得分:1)
您可以将SUM
聚合函数与GROUP BY
语句一起使用:
SELECT SUM(step_count.steps),
step_count.email,
logins.FirstName,
logins.LastName
FROM step_count, logins
WHERE step_count.email=logins.email
GROUP BY step_count.email, logins.FirstName, logins.LastName
通过这种方式,您可以按要聚合的字段进行分组,并为每个组求和一个数字字段(步骤)。
您还应该查看INNER JOIN
语句来连接表而不是使用WHERE
子句。
答案 1 :(得分:0)
您可以尝试此查询:
SELECT
sum(step_count.steps) as sum_steps,
step_count.email,
logins.FirstName,
logins.LastName
FROM
step_count
inner join logins on
step_count.email=logins.email
group by
step_count.email,
logins.FirstName,
logins.LastName
答案 2 :(得分:0)
您可以在mysql中使用SUM()函数。 - https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum - https://www.tutorialspoint.com/mysql/mysql-sum-function.htm 上面提供了一些参考资料以获取更多信息,但您的查询看起来像
SELECT SUM(step_count.steps), step_count.email, logins.FirstName, logins.LastName
FROM step_count, logins
WHERE step_count.email=logins.email
GROUP BY logins.email;
答案 3 :(得分:-1)
您可以通过指定要重新组合的密钥来使用+(void)callPostWithRawData:(NSDictionary *)dict withURL:(NSString
*)strUrl withToken:(NSString *)strToken withBlock:(dictionary)block
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
Please use below method.
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@/%@",WebserviceUrl,strUrl] parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:strToken forHTTPHeaderField:@"Authorization"];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
if ([responseObject isKindOfClass:[NSData class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if ((long)[httpResponse statusCode]==201) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"201" forKey:@"Code"];
if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog(@"%@",[dictionary objectForKey:@"Location"]);
[dict setObject:[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"Location"]] forKey:@"Id"];
block(dict);
}
}
else if ((long)[httpResponse statusCode]==200) {
//Leave Hours Calculate
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
block(serializedData);
}
else{
}
}
else if ([responseObject isKindOfClass:[NSDictionary class]]) {
block(responseObject);
}
} else {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:ServerResponceError forKey:@"error"];
block(dict);
}
}] resume];
}
,并使用GROUP BY
上的SUM()
来获取groupBy中指定的密钥的总步数。
更多信息:https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html