【发布时间】:2015-07-24 09:59:10
【问题描述】:
我有使用日期格式的日期范围的日期数。现在,我想从那个日期起只有几年。
假设某人,给出日期范围from: 01-01-2010 to 09-05-2019.
我希望从此日期起只有years,所以我会将它们包含在我的 sql 查询中。
如果今年指定了范围,则查询仅适用于 2015,但我如何才能在所有年份中仅在范围内使用它?
这里是查询:
$sql = "SELECT
MONTH (transaction_date),
SUM(case when finance_type != 'Deposit' then amount else 0 end) AS total_amount,
SUM(case when finance_type = 'Deposit' then amount else 0 end) AS deposit,
SUM(case when finance_type != 'Deposit' AND amount < 0 then amount else 0 end) AS expenses,
SUM(case when finance_type != 'Deposit' AND amount > 0 then amount else 0 end) AS earning,
SUM(case when finance_type != 'Deposit' AND amount!='' then num_contract else 0 end) AS total_contracts,
transaction_date
FROM
`tbl_finanace`
WHERE
transaction_date LIKE '%2015%'
AND user_id = '15'
GROUP BY
MONTH (transaction_date)
ORDER BY
MONTH (transaction_date)";
【问题讨论】:
-
transaction_date是数据库中的字符串/文本/blob 类型吗?如果是,那么您真的应该将其更改为整数或某些日期/日期时间类型。
标签: php mysql sql-server date