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. 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. 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:
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
here sqlfiddle as example : http://sqlfiddle.com/#!9/f5dbe/10