记录列出基于表数据Mysql的错误结果

时间:2015-04-13 10:58:26

标签: php mysql sql

我的表格中包含名为transaction_dateamount的字段。

基本上,我是根据amount列计算收入和费用,该列中包含有多少条记录。

现在,问题是我无法根据年份和月份填充我的记录。还有他们的计算。

假设,我有2015年和数据库只有2015年的jan,feb,march和4月的记录。所以,我想只用这几个月来填充这些记录。

我的查询正在运行,但它没有计算出正确的结果。

这就是我的尝试:

SELECT
    SUM(amount) AS total_amount,
    SUM(IF(amount < 0, amount, 0)) AS expenses,
    SUM(IF(amount > 0, amount, 0)) AS earning,
    transaction_date
FROM
    `tbl_finanace`
WHERE
    transaction_date LIKE '%2015%' 
AND user_id = 15
GROUP BY
    MONTH (transaction_date)
ORDER BY
    transaction_date

以下是此查询的输出:

total_amount expenses       earning     transaction_date 
-4120.25    -36289.00       32168.75    2015-01-02
-591.30     -8504.55        7913.25     2015-02-01
-3270.60    -12312.85       9042.25     2015-03-01
-51.90      -211.90         160.00      2015-04-01

我的结果应该是这样的:

total_amount expenses       earning     transaction_date 
-114.80     -1051.80        937.00      2015-01-02
25.40       -174.60         200.00      2015-02-01
34.90       -165.10         200.00      2015-03-01
-51.90      -211.90         160.00      2015-04-01

如果我在下面使用该日期的多个条目的单个日期,它仅显示该日期的正确结果但是我需要根据当年的年份和记录以及正确的计算得出这些结果。

查询:

SELECT
    SUM(amount) AS total_amount,
    SUM(IF(amount < 0, amount, 0)) AS expenses,
    SUM(IF(amount > 0, amount, 0)) AS earning,
    transaction_date
FROM
    `tbl_finanace`
WHERE
    transaction_date LIKE '%2015-01-02%'
AND user_id = 15
GROUP BY
    YEAR (transaction_date)
ORDER BY
    transaction_date

结果:

-114.80   -1051.80  937.00  2015-01-02

以下是三月和四月的一些矿场记录,仅供演示之用。这些是原始记录。

包含样本数据的表结构:

CREATE TABLE `tbl_finanace` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `finance_type` varchar(225) NOT NULL,
  `finance_date_time` varchar(225) NOT NULL,
  `transaction_date` date NOT NULL,
  `description` text NOT NULL,
  `amount` decimal(10,2) NOT NULL,
  `user_id` int(11) NOT NULL,
  `contract` varchar(225) NOT NULL,
  `strike` varchar(225) NOT NULL,
  `expiry_time` varchar(225) NOT NULL,
  `profit` varchar(225) NOT NULL,
  `contact_amount` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `finance_date_time` (`finance_date_time`)
) ENGINE=InnoDB AUTO_INCREMENT=3129 DEFAULT CHARSET=latin1;

INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4957', 'Sell', 'Sell--2015-03-25--10:09:17', '2015-03-25', '2 EUR/USD >1.0970 (12PM) @ 46', '-108.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4958', 'Fee Payment', 'Fee Payment--2015-03-25--10:09:17', '2015-03-25', '2 EUR/USD >1.0970 (12PM) @ 46', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4959', 'Settlement Payout', 'Settlement Payout--2015-03-25--12:00:06', '2015-03-25', '2 SHORT EUR/USD >1.0970 (12PM) @ 46', '0.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4960', 'Buy', 'Buy--2015-03-31--08:29:35', '2015-03-31', '2 EUR/JPY >129.00 (3PM) @ 59.25', '-118.50', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4961', 'Fee Payment', 'Fee Payment--2015-03-31--08:29:35', '2015-03-31', '2 EUR/JPY >129.00 (3PM) @ 59.25', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4962', 'Sell', 'Sell--2015-03-31--10:10:06', '2015-03-31', '2 USD/CAD >1.2720 (3PM) @ 46', '-108.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4963', 'Fee Payment', 'Fee Payment--2015-03-31--10:10:06', '2015-03-31', '2 USD/CAD >1.2720 (3PM) @ 46', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4964', 'Buy to Close', 'Buy to Close--2015-03-31--10:58:04', '2015-03-31', '2 USD/CAD >1.2720 (3PM) @ 12', '176.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4965', 'Fee Payment', 'Fee Payment--2015-03-31--10:58:04', '2015-03-31', '2 USD/CAD >1.2720 (3PM) @ 12', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4966', 'Sell to Close', 'Sell to Close--2015-03-31--13:47:00', '2015-03-31', '2 EUR/JPY >129.00 (3PM) @ 6', '12.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4967', 'Fee Payment', 'Fee Payment--2015-03-31--13:47:00', '2015-03-31', '2 EUR/JPY >129.00 (3PM) @ 6', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4968', 'Sell', 'Sell--2015-04-01--09:33:50', '2015-04-01', '2 EUR/JPY >129.20 (3PM) @ 41', '-118.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4969', 'Fee Payment', 'Fee Payment--2015-04-01--09:33:50', '2015-04-01', '2 EUR/JPY >129.20 (3PM) @ 41', '-1.80', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4970', 'Buy to Close', 'Buy to Close--2015-04-01--09:43:41', '2015-04-01', '1 EUR/JPY >129.20 (3PM) @ 20', '80.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4971', 'Fee Payment', 'Fee Payment--2015-04-01--09:43:41', '2015-04-01', '1 EUR/JPY >129.20 (3PM) @ 20', '-0.90', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4972', 'Buy to Close', 'Buy to Close--2015-04-01--09:45:13', '2015-04-01', '1 EUR/JPY >129.20 (3PM) @ 20', '80.00', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4973', 'Fee Payment', 'Fee Payment--2015-04-01--09:45:13', '2015-04-01', '1 EUR/JPY >129.20 (3PM) @ 20', '-0.90', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4974', 'Buy', 'Buy--2015-04-01--22:28:01', '2015-04-01', '2 AUD/USD >.7600 (11AM) @ 44.25', '-88.50', '15', '', '', '', '', '0');
INSERT INTO `financDB`.`tbl_finanace` (`id`, `finance_type`, `finance_date_time`, `transaction_date`, `description`, `amount`, `user_id`, `contract`, `strike`, `expiry_time`, `profit`, `contact_amount`) VALUES ('4975', 'Fee Payment', 'Fee Payment--2015-04-01--22:28:01', '2015-04-01', '2 AUD/USD >.7600 (11AM) @ 44.25', '-1.80', '15', '', '', '', '', '0');

2 个答案:

答案 0 :(得分:1)

乍一看:

  • transaction_date是一个日期。那么为什么要在其上使用字符串操作(LIKE '%2015-01-02%')?你不应该。
  • 您正在使用MySQL功能。您按月或年分组,但显示transaction_date。因此,显示的transaction_date是该月或年中的随机日期。因此,尽管2015-01-02有一条线,该线实际上显示了2015-01或2015年整个月的价值。

希望这可以帮助您找到错误。

答案 1 :(得分:1)

如果您想按月搜索结果,则需要group by月份。此外,您不应将like与日期/时间列一起使用。我希望这样的事情:

SELECT month(transaction_date),
       SUM(amount) AS total_amount,
       SUM(IF(amount < 0, amount, 0)) AS expenses,
       SUM(IF(amount > 0, amount, 0)) AS earning   
FROM `tbl_finanace`
WHERE transaction_date >= '2015-01-01' AND user_id = 15
GROUP BY MONTH(transaction_date)
ORDER BY MONTH(transaction_date);

这似乎适用于SQL Fiddle