简体   繁体   English

如何减去从表中选择的两个值

[英]How can I subtract two values selected from a table

I have a MySQL table, like this: 我有一个MySQL表,像这样:

CREATE TABLE IF NOT EXISTS `ladderStandard` (
  `charId` mediumint(8) unsigned NOT NULL,
  `poeRank` smallint(5) unsigned NOT NULL,
  `lvl` tinyint(3) unsigned NOT NULL,
  `exp` int(10) unsigned NOT NULL,
  `ladderTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `ladderStandard` (`charId`, `poeRank`, `lvl`, `exp`, `ladderTime`) VALUES
(10000, 10000, 91, 2108226870, '2015-02-06 23:37:11'),
(10001, 10001, 91, 2108221545, '2015-02-06 23:37:11'),
(10002, 10002, 91, 2108219833, '2015-02-06 23:37:11'),
(10003, 10003, 91, 2108192924, '2015-02-06 23:37:11'),
(10004, 10004, 91, 2108154502, '2015-02-06 23:37:11'),
(10005, 10005, 91, 2108153763, '2015-02-06 23:37:11'),
(10000, 9998, 91, 2108226870, '2015-02-06 23:58:21'),
(10001, 9999, 91, 2108221545, '2015-02-06 23:58:21'),
(10002, 10000, 91, 2108219833, '2015-02-06 23:58:21'),
(10003, 10001, 91, 2108192924, '2015-02-06 23:58:21'),
(10004, 10002, 91, 2108154502, '2015-02-06 23:58:21'),
(10005, 10003, 91, 2108153763, '2015-02-06 23:58:21'),

I have two queries: 我有两个查询:

SELECT charId, exp FROM ladderStandard WHERE ladderTime = '2015-02-06 23:37:11';
SELECT charId, exp FROM ladderStandard WHERE ladderTime = '2015-02-06 23:58:21';

Now I want subtract these two queries to get charId and exp.from.first.query - exp.from.second.query 现在我想减去这两个查询以获得charIdexp.from.first.query - exp.from.second.query

Here is an example on sqlfiddle.com . 这是sqlfiddle.com上的示例

You need to join both the queries then do the math 您需要同时加入两个查询,然后进行数学运算

SELECT a.charid,
       a.exp - b.exp
FROM   (SELECT charId,
               exp
        FROM   ladderStandard
        WHERE  ladderTime = '2015-02-06 23:37:11') a
       JOIN (SELECT charId,
                    exp
             FROM   ladderStandard
             WHERE  ladderTime = '2015-02-06 23:58:21') b
         ON a.charid = b.charid 

SqlFiddle Demo SqlFiddle演示

Use a self-JOIN. 使用自联接。 The WHERE clause will restrict each subset of the table to the specific time, and the ON clause links the corresponding rows. WHERE子句将表的每个子集限制为特定时间, ON子句链接相应的行。

SELECT a.charid, a.exp - b.exp AS diff
FROM ladderStandard AS a
JOIN ladderStandard AS b ON a.charid = b.charid
WHERE a.ladderTime = '2015-02-06 23:37:11'
AND b.ladderTime = '2015-02-06 23:58:21'

DEMO 演示

In your sample data, the exp values are the same for the same ID in both groups. 在示例数据中,两组中相同ID的exp值相同。 So subtracting one from the other will just result in zero. 因此从另一个减去一个只会得到零。

However, on the assumption that your actual data has different values, here is a way to perform the operation without joining the table. 但是,假设您的实际数据具有不同的值,这是一种无需连接表即可执行操作的方法。

SELECT  CharId,
        Sum( case LadderTime
             when :SecondDate then exp
             else -exp end )as SumExp
FROM    LadderStandard
where   LadderTime in( :FirstDate, :SecondDate )
group by CharID;

As expected, this SQL Fiddle shows all zeros except for CharID = 10291, which has an entry in the second group but not the first. 不出所料,此SQL Fiddle显示了所有零,但CharID = 10291除外,后者在第二组中有一个条目,但在第一组中没有。 In that case, the calculation is 0 - exp.from.second.query which is just the negative of the exp value. 在这种情况下,计算结果为0 - exp.from.second.query ,它只是exp值的负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM