小编Tom*_*Mac的帖子

MySQL存储过程错误处理

我相信MySQL目前没有任何东西可以访问SQLSTATEMySQL存储过程中最后执行的语句.这意味着当SQLException在存储过程中引发泛型时,很难/不可能导出错误的确切性质.

有没有人有一个解决方法来导出SQLSTATEMySQL存储过程中的错误,该错误不涉及为每个可能的SQLSTATE声明一个处理程序?

例如 - 假设我试图返回一个error_status,它超出了通用的"SQLException发生在这个BEGIN....END块的某个地方",如下所示:

DELIMITER $$

CREATE PROCEDURE `myProcedure`(OUT o_error_status varchar(50))
MY_BLOCK: BEGIN

 DECLARE EXIT handler for 1062 set o_error_status := "Duplicate entry in table";
 DECLARE EXIT handler for 1048 set o_error_status := "Trying to populate a non-null column with null value"; 
-- declare handlers ad nauseum here....

 DECLARE EXIT handler for sqlexception set o_error_status:= "Generic SQLException. You'll just have to figure out the SQLSTATE yourself...." ;

-- Procedure logic that …
Run Code Online (Sandbox Code Playgroud)

mysql error-handling stored-procedures

13
推荐指数
2
解决办法
6万
查看次数

MySQL抓取用户的所有帖子和每个帖子的所有喜欢(加入,分组,计数等)

我正在构建一个论坛软件,我试图通过特定的"个人资料"获取所有帖子,抓住所有已经"喜欢"每个帖子("喜欢")的个人资料,然后第三次获取个人资料每个帖子都喜欢每个帖子(profile_name等等).

Table 1 - posts
---------------
post_id
profile_id (original poster)
content
...

Table 2 - like
--------------
profile_id
post_id

Table 3 - profiles
------------------
profile_id
profile_name
...
Run Code Online (Sandbox Code Playgroud)

一些规则:

  • 每个喜欢简直的关系post_idprofile_id
  • 个人资料不能像帖子一样多次
  • 个人资料不能像自己的帖子一样

如果使用PHP获取数据,我会坚持将所有类似profile_ids("likers")存储到数组中的步骤.这是我到目前为止:

SELECT post.*, COUNT(`like`.profile_id) AS like_count 
  FROM post LEFT OUTER JOIN `like` ON post.post_id = `like`.post_id 
    WHERE post.profile_id = "'.$this->profile_id.'" GROUP BY post.post_id
Run Code Online (Sandbox Code Playgroud)

提前致谢.如果我发现任何错误,或者有任何错误指出,我会尝试更新.

php mysql join group-by relational

2
推荐指数
1
解决办法
1581
查看次数