Table frounds:
ID | oname | fraised
----- ------------ -----------------------------
1 | linkedcare | $15,000,000
2 | Shankai Sports International | $45,000,000
3 | Puma | $95,000,000
4 | Nike Women | $75,000,000
PHP代码
$result = mysqli_query($con,'SELECT SUM(fraised) AS SUM FROM frounds');
$row1 = mysqli_fetch_assoc($result);
$sum = $row1['SUM'];
echo $sum;
上面的输出是' 0'
我想要的结果
' fraised'的总和以现场为例,230,000,000' 但我得到的输出是' 0'
哪里错了?
答案 0 :(得分:0)
不是最佳做法,但您可以删除货币符号和逗号,将<label data-domain="domain.com">
<input type="text" placeholder="exampledomain" />
<label>
转换为数字,然后求和:
fraised
继续前进,使用数字列(可能是十进制)来存储您的货币信息。您应该将上述查询视为临时黑客,直到您可以修复表格设计。
答案 1 :(得分:0)
有两种方法可以做到。
首先:如果您想在表格中保留fraised
的格式,请使用php代码为您做总结。
$result = mysqli_query($con, 'SELECT fraised FROM frounds');
$sum = 0;
while ($row1 = mysqli_fetch_assoc($result)) {
$sum += str_replace('$','',$row1['fraised']);
}
echo $sum;
第二:将表格中的数据类型更改为int
,在这种情况下,您无法以$15,000,000
格式存储数据,但可以使用php number format来显示欲望格式的数据
答案 2 :(得分:0)
由于您使用的是string
而不是number
,因此您必须更改数据类型,或者需要从字符串中删除$
和,
。
查询:
SELECT SUM(cast(REPLACE(substring(fraised,2),',','') as decimal)) AS SUM FROM frounds;
您可以使用以下代码:
$result = mysqli_query($con,"SELECT SUM(cast(REPLACE(substring(fraised,2),',','') as decimal)) AS SUM FROM frounds;");
$row1 = mysqli_fetch_assoc($result);
$sum = $row1['SUM'];
echo $sum;
答案 3 :(得分:0)
尝试此查询,它将适合您
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define ERR(str) {printf("%s errno says: %s\n", str, strerror(errno));}
int getFTPcode(int sock){
char response[2550];
if(0 > recv(sock, response, 2550, 0)){
ERR("ftp response failed to recv");
return 0;
}
printf("%s\n", response);
while(1){
char msg[2000];
printf("Enter command:");
scanf("%s", msg);
if(0 > send(sock, msg, strlen(msg), 0)){
ERR("failed to send to server");
return 0;
}
if(0 > recv(sock, response, 2000, 0)){
ERR("failed to recv from server");
return 0;
}
printf("Response: %s\n", response);
}
printf("%s\n", response);
}
int connToOrig(char* ip, short port){
struct sockaddr_in addrin;
memset(&addrin, '0', sizeof(struct sockaddr_in));
addrin.sin_family = AF_INET;
addrin.sin_port = htons(port);
if(inet_pton(AF_INET, ip, &addrin.sin_addr) < 0){
ERR("inet_pton failed");
return 0;
}
int sock;
if(0 > (sock = socket(AF_INET, SOCK_STREAM, 0))){
ERR("socket() failed");
return 0;
}
if(connect(sock, (struct sockaddr*)&addrin, sizeof(addrin)) < 0){
ERR("connection to the origin server failed");
return 0;
}
printf("connection succesfull\n");
getFTPcode(sock);
return 1;
}
int main(int argc, char** argv){
connToOrig(argv[1], 21);
return 0;
}
更新后,您的代码将是
select SUM(REPLACE(REPLACE(fraised,'$',''), ',' , '')) as sum from frounds
原因:由于您的列$result = mysqli_query($con,'select SUM(REPLACE(REPLACE(fraised,'$',''), ',' , '')) as sum from frounds');
$row1 = mysqli_fetch_assoc($result);
$sum = $row1['sum'];
echo $sum;
的数据类型具有以下格式fraised
的值,因此您获得零。在我的解决方案中,我重新审核了'$15,000,000'
和$
。