我正在尝试在 MySQL 中SET
3个变量,并获取其中两个变量的和。
第一个变量@cFollow
和@cComment
,应该各自返回一个整数值(返回多少行的计数);第三个是这两个整数的和。
这是我的 SQL :
SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));
SET @total = @cFollow + @cComment;
SELECT @total;
当我在PHPMyAdmin上对其进行测试时,它返回了正确的值,并且运行良好。但是,当我在 PHP 中对其进行测试时,它返回了一个空数组。
这是我的 PHP :
$holdPoint = (int)Input::get("hold_point");
$_SQL = "
SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));
SET @total = @cFollow + @cComment;
SELECT @total;";
$_PARAMS = [":unix" => $holdPoint, ":user" => $user_id];
$check = DB::getInstance()->queryPro($_SQL, $_PARAMS);
var_dump($check);
这是该var_dump
的结果:
array(0){} // not very impressive...
// should be something like int(1) instead
我整夜都在搜索如何通过 MySQL 查询在 PHP 中返回变量,这是我所了解的。 / p>
感谢所有帮助,
干杯。
答案 0 :(得分:0)
此答案并不是真正的答案,而是更多的评论。
另请注意,您的查询
SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));
SET @total = @cFollow + @cComment;
SELECT @total;
最有可能被重写为一个查询
SELECT
SUM(alias.c) AS total
FROM (
SELECT COUNT(*) AS c FROM followers WHERE unix > :unix AND following = :user
UNION ALL
SELECT COUNT(*) AS c FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user)
) AS alias