M
M
Mikhail Konyukhov2012-12-03 18:27:01
MySQL
Mikhail Konyukhov, 2012-12-03 18:27:01

Problem with MairaDB stored procedure and count(*) subquery. How to decide?

There is a database: MariaDB 5.5.27 (MySQL fork)
There is a procedure:

CREATE PROCEDURE koshka_get_day_records (IN user_id INT, IN country INT, IN udate DATETIME)
BEGIN
  set @weekday = DATE_FORMAT(udate, '%w');
  set @monthday = DATE_FORMAT(udate, '%d');
  set @yearday = DATE_FORMAT(udate,'%m-%d');
  set @datetime = udate;
  set @mdl = DATE_FORMAT(udate,'%d');
  IF (DATE_FORMAT(udate,'%d')=DATE_FROMAT(LAST_DAY(udate),'%d')) THEN set @mdl = '32';
  END IF;
  SELECT * 
  FROM koshka_calendar_records 
  WHERE user_id = @user_id 
        AND DATE_FORMAT(started_at,"%Y-%m-%d")<=DATE_FORMAT(@datetime,"%Y-%m-%d")
        AND (DATE_FORMAT(finished_at,"%Y-%m-%d")>DATE_FORMAT(@datetime,"%Y-%m-%d") OR finished_at IS NULL)
        AND (DATE_FORMAT(datetime,"%Y-%m-%d") = DATE_FORMAT(@datetime,"%Y-%m-%d") 
             OR weekday = @weekday 
             OR monthday = @monthday 
             OR DATE_FORMAT(yearday,"%m-%d") = DATE_FORMAT(@yearday,"%m-%d") 
             OR
             	(datetime IS NULL AND weekday IS NULL AND monthday IS NULL AND yearday IS NULL AND work_or_holyday IS NULL AND workday_counter IS NULL) 
             OR monthday = @mdl 
             OR
        		(work_or_holyday != (
                  select count(*) from koshka_work_calendar WHERE type IN ("weekend","holyday") 
                  AND country = @country 
                  AND DATE_FORMAT(date,"%Y-%m-%d")=DATE_FORMAT(@datetime,"%Y-%m-%d")
                )) 
             OR
         		workday_counter = (
                  select CAST(DATE_FORMAT(@datetime,"%d") as int)-count(*) from koshka_work_calendar 
                  WHERE date <= @datetime 
                  AND DATE_FORMAT(date,"%Y-%m")=DATE_FORMAT(@datetime,"%Y-%m") 
    AND country = @country 
    AND [email protected]_id)
   	     );
END

After we do:
delimiter |
...код процедуры....
|

An error occurs:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) from koshka_work_calendar WHERE type IN (&quot;weekend&quot;,&quot;holyday&quot;) 
              ' at line 23

I can't figure out what the problem is. How can it be solved?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dobersoft, 2012-12-04
@piromanlynx

why not make this subquery at the beginning with the return value assigned to a temporary variable? doesn't that break the logic? Perhaps this will help localize the problem.
select count(*) from koshka_work_calendar WHERE type IN ("weekend","holyday")
AND country = country
AND DATE_FORMAT(date,"%Y-%m-%d")=DATE_FORMAT(@datetime,"%Y-% m-%d")

M
mark_ablov, 2012-12-03
@mark_ablov

count(id)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question