没有拿起where子句 - 已排序过程MySql

时间:2016-07-20 11:33:14

标签: php mysql stored-procedures

ID, UserID, NumberOfHours, WeekCommencing

该过程为每个用户添加一个新行,当任何一个用户有12行时,需要删除针对该用户的最早行并添加新行。否则只需添加一行。 这保留了每个用户过去12周的运行记录。

问题select count”由于某种原因没有选择where子句?

    CREATE DEFINER PROCEDURE `Avghours`(IN `Userid` INT, IN `Hours` DECIMAL(5,2), IN `WCdate` DATE)

    BEGIN
    DECLARE NUM INT;
    SELECT COUNT(*)
    FROM avghours
    WHERE UserID = Userid
    INTO NUM;

    IF NUM >= 12 then
    DELETE FROM avghours WHERE UserID = Userid
    ORDER BY UserID LIMIT 1;
    END IF;
    INSERT INTO avghours(
    UserID
   ,NumberOfHours
    ,WeekCommencing)
    VALUES(
    Userid
   ,Hours
   ,WCdate);

   END 

1 个答案:

答案 0 :(得分:0)

请检查一下你会明白

   mysql> DELIMITER $$
mysql> CREATE PROCEDURE `sp_testyt`()
    -> BEGIN
    -> 
    -> DECLARE NUMM INT;
    -> SELECT count(*) FROM gg into NUMM;
    -> SELECT @NUMM;
    -> 
    -> END $$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> call sp_testyt();
+-------+
| @NUMM |
+-------+
| NULL  |
+-------+
1 row in set (0.00 sec)

-----------纠正了SP ------------

mysql> DELIMITER $$
mysql> CREATE PROCEDURE `sp_testtt`()
    -> BEGIN
    -> DECLARE NUM INT;
    -> SELECT count(*) FROM gg into @NUM;
    -> SELECT @NUM;
    -> 
    -> END $$
Query OK, 0 rows affected (0.02 sec)

mysql> DELIMITER ;
mysql> CALL sp_testtt();
+------+
| @NUM |
+------+
|    4 |
+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

更新删除查询

DELETE FROM avghours WHERE ID in (SELECT ID FROM avghours WHERE Userid=Userid ORDER BY ID LIMIT 1)