5 2014-02-24 14:59:28 7 NULL
5 2014-02-24 15:32:55 6 -1
5 2014-03-08 10:32:19 60 54
5 2014-03-08 10:40:23 20 -40
5 2014-03-08 10:44:22 21 1
5 2014-03-08 15:05:54 2 -19
5 2014-03-08 15:06:10 25 23
但我得到了:
5 2014-02-24 02:59:28 7 NULL NULL
5 2014-02-24 03:32:55 6 -1 -14.2857%
5 2014-03-08 03:05:54 2 -19 -90.4762%
5 2014-03-08 03:06:10 25 23 1150.0000%
5 2014-03-08 10:32:19 60 54 900.0000%
5 2014-03-08 10:40:23 20 -40 -66.6667%
5 2014-03-08 10:44:22 21 1 5.0000%
这只是stock_id的例子5.其他人也错了时间。 我的代码如下:
select stock_id,date_format(date,'%Y-%m-%d %h:%i:%s') as date
, price,pxchange,concat(round(pxpct*100,4),'%') pxpct
from (select
case when stock_id <> @pxticker then
@pxclose := null end,
p.*,
(price-@pxclose) as pxchange,
(price-@pxclose)/@pxclose as pxpct,
(@pxclose := price),
(@pxticker := stock_id)
from quotations p
cross join
( select
@pxclose := null,
@pxticker := stock_id
from quotations
order by stock_id, date limit 1) as a
order by stock_id, date ) as b
order by stock_id, date asc
但出了什么问题?我添加(日期,'%Y-%m-%d%h:%i:%s')对,不是吗?谁知道问题出在哪里?
编辑 - 附加问题和代码
SELECT
stock_id, stocks.name,
date_format( date, '%Y-%m-%d %H:%i:%s' ) AS date,
price,
pxchange,
concat( round( pxpct *100, 4 ) , '%' ) pxpct,
stocks.stockmarket,
stockmarkets.id
FROM (
SELECT
CASE WHEN stock_id <> @pxticker
THEN @pxclose := NULL
END , p . * ,
(price - @pxclose) AS pxchange,
(price - @pxclose) / @pxclose AS pxpct,
(@pxclose := price),
(@pxticker := stock_id)
FROM quotations p
CROSS JOIN
(
SELECT @pxclose := NULL , @pxticker := stock_id
FROM quotations
ORDER BY stock_id, date
LIMIT 1
) AS a
ORDER BY stock_id, date
) AS b
LEFT JOIN
stocks ON stock_id = stocks.id
LEFT JOIN
stockmarkets ON stockmarkets.id = stocks.stockmarket
WHERE stocks.stockmarket = (SELECT id FROM stockmarkets WHERE short ='NASDAQ')
GROUP BY stock_id
ORDER BY stock_id, MIN(date) ASC
答案 0 :(得分:1)
日期函数需要一个大写字母H,输出为24小时格式,否则它将输出AM / PM日期,没有AM / PM符号(即仅从0-11小时开始)。