I have SQL code that returns a set of results, as expected in both MySQL clients, namely MySQL Workbench and Sequel Pro. However, when I put the SQL code in a stored procedure (SP), it returns a result set with one row (different from the previous one) in Sequel Pro. SP still works fine on MySQL Workbench (the right set of results). I do not understand why. The same SQL code, the same database, but different result sets on two different SQL clients.
My ultimate goal is to call this SP from a PHP file. When I do this, I get the same result from Sequel Pro. What do you think might be the problem?
Here is my SQL code:
SET @prev_val:=24;
SET @num := 0;
SELECT @prev_date := DATE(MAX(logDate)) FROM EmotionsDB.`Log`;
SELECT @prev_date_outerLoop := DATE(MAX(logDate)) FROM EmotionsDB.`Log`;
SELECT dateOfEmotion, emotionID
FROM
(
SELECT dateOfEmotion, diffHour, emotionID,
@num := IF(dateOfEmotion = @prev_date_outerLoop, @num + 1, 1) AS row_number,
@prev_date_outerLoop := dateOfEmotion AS previousDate
FROM
(
SELECT DATE(logDate) AS dateOfEmotion, TIME(logDate) AS timeOfEmotion, emotionID,
IF(DATE(logDate) = @prev_date, @prev_val - HOUR(logDate), 24 - HOUR(logDate)) AS diffHour,
@prev_val:=HOUR(logDate) AS previousHour,
@prev_date:=DATE(logDate) AS previousDate
FROM EmotionsDB.Log
WHERE DATE(logDate) <> DATE(CURDATE()) AND MONTH(logDate) = MONTH(CURDATE())
ORDER BY DATE(logDate) DESC, TIME(logDate) DESC
) AS InnerSubQuery
ORDER BY dateOfEmotion DESC, diffHour DESC
) AS OuterSubQuery
WHERE row_number = 1;
, MySQL Workbench ( , "dateOfEmotion" "emotionID" ):
2014-02-17 6
2014-02-14 2
2014-02-13 2
2014-02-07 5
2014-02-06 2
2014-02-05 0
2014-02-02 3
, SP Sequel Pro ( "@prev_date: = DATE (MAX (logDate))" ):
2014-02-17
.