[英]MYSQL Records Fetch for 2 dates
Currently i require that if time is before noon for current day, then it should take the item_2 value of prev day for current day only. 目前,我要求如果时间是当天中午之前,那么它应该仅将当天的prev day的item_2值作为。 for others it will take, their proper values.
对于其他人,这将是他们的正确价值观。
id | item_1 | item_2 | date
1 205 3 2015-04-07
2 215 35 2015-04-06
3 225 15 2015-04-05
4 235 315 2015-04-04
Expected result wen time is before noon 预期结果时间是中午之前
id | item_1 | item_2 | sum | date
1 205 35 240 2015-04-07
2 215 35 250 2015-04-06
3 225 15 240 2015-04-05
4 235 31 266 2015-04-04
Expected result wen time is after noon 预期结果是下午中午
id | item_1 | item_2 | sum | date
1 205 3 208 2015-04-07
2 215 35 250 2015-04-06
3 225 15 240 2015-04-05
4 235 31 266 2015-04-04
Date and time can be used from php and how to manage for prev and current day record in same query. 可以从php使用日期和时间,以及如何在同一查询中管理上一个和当前日期的记录。 i tried with sub query, but i think it can be done in other way also.
我尝试了子查询,但我认为也可以通过其他方式完成。
Thanks in advance. 提前致谢。
In this case you have to use CASE in your query: 在这种情况下,您必须在查询中使用CASE :
try this: 尝试这个:
SET @time := 'BFNOON';
SET @date := '2015-04-07 00:00:00';
SELECT id, item_1, item_2, (item_1 + item_2) AS sum_item, dates FROM (
SELECT id, item_1,
(CASE
WHEN (@time = 'BFNOON' AND dates = @date) THEN
(
SELECT item_2 FROM records WHERE dates < @date ORDER BY dates DESC LIMIT 1
) ELSE
item_2
END ) item_2
, dates
FROM records ) records
Note: remove declaration and replace @time = 'BFNOON' to check before or after noon and @date with your date value of today 注意:删除声明,并将@time ='BFNOON'替换为中午和@date之前或之后的今天日期值
here sqlfiddle as example : http://sqlfiddle.com/#!9/f5dbe/10 这里以sqlfiddle为例: http : //sqlfiddle.com/#!9/f5dbe /10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.