MySQL根据记录求和和相减?

发布于 2024-12-01 23:37:44 字数 293 浏览 2 评论 0原文

我在 mysql 中有一条记录,其中包含如下内容:

+15,+23,-21,+23,-3 

我想要做的就是在 SQL 查询中,这些值根据它们的符号进行添加和减去。

我知道 SUM(),但是减法呢?基本上,我需要一些可以在一个查询中执行的东西,如下例所示:

SELECT SUM(result) AS 'total' FROM mytable  WHERE year='2011' AND month='10';

I have a record in mysql which contains something like this:

+15,+23,-21,+23,-3 

All I want to do is that in a SQL query these values get added and subtracted according to their signs.

I know of SUM(), but what about the subtraction? Basically, I need something that can be executed in one query, like the following example:

SELECT SUM(result) AS 'total' FROM mytable  WHERE year='2011' AND month='10';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-12-08 23:37:44

您的查询将正常工作。测试

CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `p` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

insert into test values(null, 5), (null, -1), (null,4);
select sum(p) from test;

结果为8。

Your query will work correctly. Tested on

CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `p` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

insert into test values(null, 5), (null, -1), (null,4);
select sum(p) from test;

Result is 8.

郁金香雨 2024-12-08 23:37:44

您在数据库中使用逗号分隔字段。

这是一个巨大的 NONO,(My)SQL 没有很好的能力来处理这个问题。
如果每行中的值不超过 5 个,您可以执行以下操作:

尴尬代码警告

$month = mysql_real_escape_string($data['month']);
$year = mysql_real_escape_string($data['year']);
$sql = "SELECT 
  IFNULL(SUBSTRING_INDEX(result,',',1),0) 
  + IFNULL(SUBSTRING_INDEX(result,',',2),0)
  + IFNULL(SUBSTRING_INDEX(result,',',3),0)
  + IFNULL(SUBSTRING_INDEX(result,',',4),0)  
  + IFNULL(SUBSTRING_INDEX(result,',',5),0) as total_result
FROM mytable
WHERE `year` = '$year' AND `month` = '$month' ";

我不能 100% 确定 MySQL 会满意“+”符号,如果不是,您会需要通过执行 REPLACE(IFNULL(SUBSTRING_INDEX(result,',',2),0),'+','') 来过滤掉它们

,请参阅:
http://dev.mysql.com/doc /refman/5.0/en/string-functions.html#function_substring-index
http://dev.mysql.com/doc/refman /5.0/en/string-functions.html#function_replace

重构数据库
我建议您改掉 CSV 的习惯并标准化您的数据库。

将值放入不同的行(每行一个值),并将列合并为一列,并添加auto_increment整数主键名为 id 的列

您的表格将类似于:

id    result    mydate
1     15       2011-01-01
2     13       2011-01-01
3     -15      2011-01-01
...
4875  -23      2011-08-28

现在,您可以通过执行以下操作来添加特定月份的结果总和:

$sql = "SELECT sum(result) as totalresult 
FROM mytable
WHERE mydate BETWEEN '$startdate' AND '$enddate' ";

使用 Between ... and 构造而不是函数 YEAR(mydate) 和MONTH(mydate) 将允许 MySQL 使用索引来选择值。
你将一个列包装在一个函数中 MySQL 不能使用索引,这会大大减慢速度。

You are using a comma separated field in your database.

That's a huge NONO, (My)SQL is not very well equipped to deal with this.
If you have no more than 5 values in every row you can do something like:

Awkward code warning

$month = mysql_real_escape_string($data['month']);
$year = mysql_real_escape_string($data['year']);
$sql = "SELECT 
  IFNULL(SUBSTRING_INDEX(result,',',1),0) 
  + IFNULL(SUBSTRING_INDEX(result,',',2),0)
  + IFNULL(SUBSTRING_INDEX(result,',',3),0)
  + IFNULL(SUBSTRING_INDEX(result,',',4),0)  
  + IFNULL(SUBSTRING_INDEX(result,',',5),0) as total_result
FROM mytable
WHERE `year` = '$year' AND `month` = '$month' ";

I'm not 100% sure MySQL will be happy with the '+' signs, if not you'll need to filter them out by doing REPLACE(IFNULL(SUBSTRING_INDEX(result,',',2),0),'+','')

See:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Refactoring you database
I recommend that you kick the CSV habit and normalize your database.

Put the values into different rows (one value per row) and merge the month and year columns into one, and add a auto_increment integer primary key column named id

Your table will look something like:

id    result    mydate
1     15       2011-01-01
2     13       2011-01-01
3     -15      2011-01-01
...
4875  -23      2011-08-28

Now you can add the sum of results in a particular month by doing:

$sql = "SELECT sum(result) as totalresult 
FROM mytable
WHERE mydate BETWEEN '$startdate' AND '$enddate' ";

Using the between ... and construct instead of the functions YEAR(mydate) and MONTH(mydate) will allow MySQL to use an index to select the values.
You you wrap a column in a function MySQL cannot use an index slowing things down a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文