Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用存储过程获取两个表的总计数?_Mysql_Stored Procedures - Fatal编程技术网

Mysql 如何使用存储过程获取两个表的总计数?

Mysql 如何使用存储过程获取两个表的总计数?,mysql,stored-procedures,Mysql,Stored Procedures,这是我试图创建的存储过程 I need to create a stored procedure for getting the count of two table by using where clause condition, but when i try to create procedure it shows error Query : CREATE PROCEDURE bcp.getTotalCount BEGIN -- Create two integer values

这是我试图创建的存储过程

  I need to create a stored procedure for getting the count of two table by using where clause condition, but when i try to create procedure it shows error

Query : CREATE PROCEDURE bcp.getTotalCount BEGIN   -- Create two integer values  DECLARE @tableOneCount int, @tableTwoCount int  -- Get ...
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN
  -- Create two integer values
    DECLARE @tableOneCount int, @tableTwoCount' at line 2
为了更好地理解,我尝试了这样的简单sql查询

DELIMITER $$

DROP PROCEDURE IF EXISTS bcp.getTotalCount $$
CREATE PROCEDURE bcp.getTotalCount
BEGIN
  -- Create two integer values
    DECLARE @tableOneCount INT, @tableTwoCount INT

    -- Get the number of rows from the first table
    SELECT @tableOneCount = (SELECT COUNT(*) FROM candidates WHERE active=1)
    SELECT @tableTwoCount = (SELECT COUNT(*) FROM voters_enrollment WHERE active=1)

    -- Return the sum of the two table sizes
    SELECT TotalCount = @tableOneCount + @tableTwoCount
END $$

DELIMITER ;
我得到的结果是

SELECT (SELECT COUNT(*) FROM candidates WHERE active=1)+ 
       (SELECT COUNT(*) FROM voters_enrollment WHERE active=1) AS Total

像这样,我需要创建过程并调用它,通过使用简单的sql查询获得相同的结果。有人能帮我解决这个问题吗。

你必须在create语句后面加上AS

Total
10

你可以试试这个,伙计:

    DELIMITER $$

    DROP PROCEDURE IF EXISTS bcp.getTotalCount $$
    CREATE PROCEDURE bcp.getTotalCount

    AS

    BEGIN

  -- Create two integer values
    DECLARE @tableOneCount INT, @tableTwoCount INT

    -- Get the number of rows from the first table
    SELECT @tableOneCount = (SELECT COUNT(*) FROM candidates WHERE active=1)
    SELECT @tableTwoCount = (SELECT COUNT(*) FROM voters_enrollment WHERE active=1)

    -- Return the sum of the two table sizes
    SELECT TotalCount = @tableOneCount + @tableTwoCount
END $$

DELIMITER ;

原子性的MySQL事务

创建过程bcp.getTotalCount()
缺少括号。
DELIMITER $$
DROP PROCEDURE IF EXISTS bcp_getTotalCount $$
CREATE PROCEDURE bcp_getTotalCount()
BEGIN
    -- clear/initialize session variables
    SET
        @tableOneCount = 0,
        @tableTwoCount = 0;
    START TRANSACTION;
    -- get record count from the source tables
        SELECT COUNT(*) INTO @tableOneCount FROM candidates WHERE active = 1;
        SELECT COUNT(*) INTO @tableOneCount FROM voters_enrollment WHERE active = 1;
    -- return the sum of the two table sizes
        SELECT TotalCount = @tableOneCount + @tableTwoCount;
    COMMIT;
END $$
DELIMITER ;