我有这个SQL
SELECT COUNT( DISTINCT `ct`.`content_type_id`) AS `type_count`
, COUNT(`content_id`) AS `content_count`
FROM `user_to_content` `uc`
JOIN `content` `c` USING (`content_id`)
JOIN `content_type` `ct` USING (`content_type_id`)
WHERE `user_id`= 1;
我想在同一张桌子上同时获得2个数字。
此SQLfiddle文件显示上述SQL正在运行...
但是我无法让SP版本正常工作。 我似乎无法让SQLfiddle与SP合作(也许有些人可以告诉我如何),所以here是在pastebin的SP ......
无论如何,SP不会用第二个COUNT命令“编译”......
#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 '' at line 24
这是第24行
DECLARE $type_count TINYINT(1) UNSIGNED DEFAULT 0;
我在这方面的工作也是两次调用桌子上的COUNT。我不愿意。
答案 0 :(得分:0)
这是一个有一些修改的例子:
DELIMITER $$
CREATE PROCEDURE `SP_system_content_badges`
( IN `$user_id` INT(10) UNSIGNED
, IN `$content_id` INT(10) UNSIGNED
, IN `$content_type_id` INT(10) UNSIGNED
)
BEGIN
-- DECLARE $type_count TINYINT(1) UNSIGNED DEFAULT 0;
-- DECLARE $content_count TINYINT(1) UNSIGNED DEFAULT 0;
DECLARE `$type_count` INT UNSIGNED DEFAULT 0;
DECLARE `$content_count` INT UNSIGNED DEFAULT 0;
-- How many pieces of content has this person consumed
-- Is there 4 unique kinds as well
-- SELECT COUNT( DISTINCT `ct`.`content_type_id`) INTO $type_count
-- , COUNT(`content_id`) INTO $content_count
SELECT COUNT(DISTINCT `ct`.`content_type_id`), COUNT(`c`.`content_id`)
INTO `$type_count`, `$content_count`
FROM `user_to_content` `uc`
JOIN `content` `c` USING (`content_id`)
JOIN `content_type` `ct` USING (`content_type_id`)
WHERE `user_id`= `$user_id`;
SELECT `$type_count` 'TYPE_COUNT', `$content_count` 'CONTENT_COUNT';
END$$
DELIMITER ;