我怎么能在这里使用SUM?

时间:2017-12-12 17:20:56

标签: mysql sql sum

我有下表,左边有一列,其中包括一些汽车的名称,右栏中的一些数字。如何为每辆车添加所有数字?

从包含100个条目的表格中,我想获得一个包含10行的表格。

aer conditionat Logan   500
aer conditionat A6  700
aer conditionat CLK 1200
aer conditionat M5  900
aer conditionat Q7  850
aer conditionat Passat  800
aer conditionat Panamera    1850
aer conditionat C5  650
aer conditionat Astra   500
aer conditionat Duster  650
geamuri fumurii Logan   100
geamuri fumurii A6  300
geamuri fumurii CLK 700
geamuri fumurii M5  550
geamuri fumurii Q7  500
geamuri fumurii Passat  450
geamuri fumurii Panamera    900
geamuri fumurii C5  250
geamuri fumurii Astra   150
geamuri fumurii Duster  300
navigatie Logan 1000
navigatie A6    1600
navigatie CLK   1800
navigatie M5    1650
navigatie Q7    1700
navigatie Passat    1650
navigatie Panamera  2000
navigatie C5    1350
navigatie Astra 1200
navigatie Duster    1400
geamuri electrice Logan 300
geamuri electrice A6    700
geamuri electrice CLK   1300
geamuri electrice M5    600
geamuri electrice Q7    700
geamuri electrice Passat    650
geamuri electrice Panamera  1500
geamuri electrice C5    500
geamuri electrice Astra 400
geamuri electrice Duster    550
scaune incalzite Logan  300
scaune incalzite A6 600
scaune incalzite CLK    1200
scaune incalzite M5 800
scaune incalzite Q7 850
scaune incalzite Passat 1000
scaune incalzite Panamera   1500
scaune incalzite C5 450
scaune incalzite Astra  400
scaune incalzite Duster 500
oglinzi electrice Logan 100
oglinzi electrice A6    270
oglinzi electrice CLK   300
oglinzi electrice M5    280
oglinzi electrice Q7    240
oglinzi electrice Passat    190
oglinzi electrice Panamera  300
oglinzi electrice C5    150
oglinzi electrice Astra 120
oglinzi electrice Duster    110
camera marsarier Logan  400
camera marsarier A6 1300
camera marsarier CLK    1900
camera marsarier M5 1100
camera marsarier Q7 1200
camera marsarier Passat 900
camera marsarier Panamera   2000
camera marsarier C5 800
camera marsarier Astra  600
camera marsarier Duster 600
senzori parcare Logan   500
senzori parcare A6  1200
senzori parcare CLK 1400
senzori parcare M5  1000
senzori parcare Q7  1200
senzori parcare Passat  900
senzori parcare Panamera    1500
senzori parcare C5  700
senzori parcare Astra   800
senzori parcare Duster  600
pilot automat Logan 1500
pilot automat A6    2200
pilot automat CLK   2800
pilot automat M5    2300
pilot automat Q7    2000
pilot automat Passat    1900
pilot automat Panamera  3000
pilot automat C5    1800
pilot automat Astra 1600
pilot automat Duster    1700
interior piele Logan    500
interior piele A6   1300
interior piele CLK  1500
interior piele M5   1300
interior piele Q7   1200
interior piele Passat   1100
interior piele Panamera 2000
interior piele C5   900
interior piele Astra    800
interior piele Duster   1000

提到左栏的名称是“dotare”,而右栏的名称是“pret”,这就是我的尝试:

         SELECT SUBSTRING_INDEX(dotare,' ', -1 ) AS CAR,
 (SELECT SUM(pret) FROM table GROUP BY SUBSTRING_INDEX(dotare,' ', -1 ))
 FROM table ORDER BY (SELECT SUBSTRING_INDEX(dotare,' ', -1 ));

1 个答案:

答案 0 :(得分:0)

我认为你不需要嵌套查询你想要做什么。只需在同一查询中选择两个信息,您已经从同一个表中获取数据,无需查询两次:

 SELECT SUBSTRING_INDEX(dotare,' ', -1 ) AS CAR, SUM(pret) 
 FROM table 
 GROUP BY SUBSTRING_INDEX(dotare,' ', -1 )
 ORDER BY CAR;

此外,在您的order by子句中,您可以使用别名,在执行order by时已经从数据库中选择了您的数据