从 MySQL 中获取数据,介于字符串整数数据类型之间
Get data from MySQL between string integer data type
我想获取起始日期和截止日期的 sum(cost) 数据。我的 table 字段数据类型是 id 是 INT AUTO INCREMENT,dd 是 varchar,mm 是 varchar,yyyy varchar,dd 值是 01 到 31,mm 值是 01 到 12,year 值是 4 位数字年份(2016 或2015 年...,)
我想要 sum(cost) = 143
SELECT SUM(cost) FROM costing
WHERE
(( dd> CAST('05' AS SIGNED) AND mm= CAST('12' AS SIGNED) AND yyyy= CAST('2015' AS SIGNED))
AND
((dd<= CAST('23' AS SIGNED) AND mm = CAST('01' AS SIGNED) AND yyyy= CAST('2016' AS SIGNED)))
order by id
您可以像这样使用 CAST AS DATE 函数:
SELECT
SUM(cost) FROM costing
WHERE
CAST(CONCAT(yyyy,mm,dd) AS DATE) BETWEEN '2015-12-05' AND '2016-01-23'
首先你应该考虑你的数据类型。日期不应为 varchar 或整数数据类型。参考这个 http://www.w3schools.com/sql/sql_datatypes.asp
那你就可以用between了。参考这个 http://www.w3schools.com/sql/sql_between.asp
那么您的查询中就不需要 order by id
。
您的查询会喜欢
SELECT SUM(cost) FROM costing WHERE costingdate BETWEEN "5-12-2015" AND 23-1-2015"
我想获取起始日期和截止日期的 sum(cost) 数据。我的 table 字段数据类型是 id 是 INT AUTO INCREMENT,dd 是 varchar,mm 是 varchar,yyyy varchar,dd 值是 01 到 31,mm 值是 01 到 12,year 值是 4 位数字年份(2016 或2015 年...,) 我想要 sum(cost) = 143
SELECT SUM(cost) FROM costing
WHERE
(( dd> CAST('05' AS SIGNED) AND mm= CAST('12' AS SIGNED) AND yyyy= CAST('2015' AS SIGNED))
AND
((dd<= CAST('23' AS SIGNED) AND mm = CAST('01' AS SIGNED) AND yyyy= CAST('2016' AS SIGNED)))
order by id
您可以像这样使用 CAST AS DATE 函数:
SELECT
SUM(cost) FROM costing
WHERE
CAST(CONCAT(yyyy,mm,dd) AS DATE) BETWEEN '2015-12-05' AND '2016-01-23'
首先你应该考虑你的数据类型。日期不应为 varchar 或整数数据类型。参考这个 http://www.w3schools.com/sql/sql_datatypes.asp
那你就可以用between了。参考这个 http://www.w3schools.com/sql/sql_between.asp
那么您的查询中就不需要 order by id
。
您的查询会喜欢
SELECT SUM(cost) FROM costing WHERE costingdate BETWEEN "5-12-2015" AND 23-1-2015"