一个语法中的多个SUM

时间:2016-11-28 08:25:42

标签: mysql sql

我有一个小问题......

我有两个针对SQL的问题:

1:

SELECT SUM(iloscrec*ilosc/100) AS iloscsur, surowiec, Produkt
FROM receptura
  JOIN produkcjadb ON(produkcjadb.Produkt = receptura.kolor)
WHERE data_zakonczenia >= CURRENT_DATE
GROUP BY surowiec

2:

SELECT SUM(ilosccal) AS ilosc, material, jednostka
FROM surowce
WHERE magazyn='G' || magazyn='K' || magazyn='S' || magazyn='P' || magazyn='PROUT' ||
      magazyn='W'  || magazyn='OR'
GROUP BY material

现在是我的问题,我可以将这2个问题连在一起,将第二个问题中的SELECT行连接到1个问题的地方

material (secondquestion) = surowiec (from1question)

即时尝试约5小时,但我仍然有sql语法错误

提前谢谢!

2 个答案:

答案 0 :(得分:0)

不确定我是否理解这个问题。如果你提供一个脚本来创建结构并插入示例数据,如果你向我们展示了预期的结果,那将会有所帮助,但无论如何我想你只想使用子查询,例如像这样的东西:

#define FAVORITE_BIKE @"user_id=%@&bike_id=%@"
@define FAVORITE_BIKE @"{\"user_id\":\"%@\",\"bike_id\":\"%@\"}"
NSString *urlString = [NSString stringWithFormat:@"url here"];
NSString *jsonString = [NSString stringWithFormat:FAVORITE_BIKE,user_id,_idStr];
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if(str.length > 0)
{
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary *resDict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}

答案 1 :(得分:0)

啊,当然是我的错?

receptura
-surowiec
-iloscrec
-kolor

produkcjadb
-Produkt
-Ilosc

surowce
-material
-ilosccal
-magazyn

现在我需要选择

1:SUM(iloscrec*ilosc/100) from receptura and produkcjadb where Produkt=kolor
2:SUM(ilosccal) FROM surowce Where magazyn='G' || magazyn='K' || magazyn='S' || magazyn='P' || magazyn='PROUT' ||
  magazyn='W'  || magazyn='OR'
3: matched this row where material(surowce)=surowiec(receptura)

有机会吗?