$sql=select * from table;
我想知道有可能实现一次查询后,就能echo 出sta为1-9,9种情形下的point的合计值么?
如:
sta=1,合计point值为。。。
sta=2,合计point值为。。。
如果用$sql=select * from table where sta=1;之类的分9次查询,我会;但有可能一次查询就可以么?谢谢大神。确实不行,我也给分,谢谢大神。
14 个解决方案
#1
group by
#2
select sta, sum(point) as point from table group by sta
#3
select sum(point) from table group by sta;
#4
select id,SUM(point) as count from t GROUP BY sta
#5
谢谢各位大神,这还是查询出一个数组吧,那我如果想打印 sta=6 的SUM(point),我怎么写呢?
#6
遍历输出就行了啊
#7
select *,sum(point) as point from table where sta=1 group by sta;
#8
假如我查询出的结果如下:我该怎么直接调用sta=3时的 [sum(points)] 和 [sum(r_points)] ?谢谢!
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] => -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[5] => Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] => -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[5] => Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
#9
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] =>; -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[15] = Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
如果是这样的数组,你会取吗?
(
[0] => Array
(
[sta] => 0
[sum(points)] =>; -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[15] = Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
如果是这样的数组,你会取吗?
#10
谢谢回复,就是这个,我该怎么取呢?谢谢!
#11
您的数据好像键名和sta的值是一样的,但实际查询时可能不一样,如我贴的数据最后一个,sta=15时,键名为5,而不是15.
#12
算了,结贴吧
#13
你也注意到了我贴的样式中:键名和sta的值是一样的
只要你在读取结果到数组时,指定键名为 sta 就可以了
只要你在读取结果到数组时,指定键名为 sta 就可以了
#14
谢谢大神,您说的方法我不会,后来我用了比较笨的办法,还是需要遍历,然后用if(sta=5){。。。。}判断,如果不用遍历,直接根据键名直接提取值就更好了,但我没学会。
#1
group by
#2
select sta, sum(point) as point from table group by sta
#3
select sum(point) from table group by sta;
#4
select id,SUM(point) as count from t GROUP BY sta
#5
谢谢各位大神,这还是查询出一个数组吧,那我如果想打印 sta=6 的SUM(point),我怎么写呢?
#6
遍历输出就行了啊
#7
select *,sum(point) as point from table where sta=1 group by sta;
#8
假如我查询出的结果如下:我该怎么直接调用sta=3时的 [sum(points)] 和 [sum(r_points)] ?谢谢!
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] => -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[5] => Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] => -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[5] => Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
#9
Array
(
[0] => Array
(
[sta] => 0
[sum(points)] =>; -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[15] = Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
如果是这样的数组,你会取吗?
(
[0] => Array
(
[sta] => 0
[sum(points)] =>; -3
[sum(r_points)] => -1
)
[1] => Array
(
[sta] => 1
[sum(points)] => -200
[sum(r_points)] => 0
)
[2] => Array
(
[sta] => 2
[sum(points)] => -1
[sum(r_points)] => -99
)
[3] => Array
(
[sta] => 3
[sum(points)] => 0
[sum(r_points)] => 108.3333
)
[4] => Array
(
[sta] => 4
[sum(points)] => 300
[sum(r_points)] => 0
)
[15] = Array
(
[sta] => 15
[sum(points)] => 0
[sum(r_points)] => 0
)
)
如果是这样的数组,你会取吗?
#10
谢谢回复,就是这个,我该怎么取呢?谢谢!
#11
您的数据好像键名和sta的值是一样的,但实际查询时可能不一样,如我贴的数据最后一个,sta=15时,键名为5,而不是15.
#12
算了,结贴吧
#13
你也注意到了我贴的样式中:键名和sta的值是一样的
只要你在读取结果到数组时,指定键名为 sta 就可以了
只要你在读取结果到数组时,指定键名为 sta 就可以了
#14
谢谢大神,您说的方法我不会,后来我用了比较笨的办法,还是需要遍历,然后用if(sta=5){。。。。}判断,如果不用遍历,直接根据键名直接提取值就更好了,但我没学会。