MySQL 从 SELECT 语句返回值

发布于 2024-09-24 00:03:42 字数 769 浏览 4 评论 0原文

我不断收到此错误: 结果由多行组成

我有这个功能:

DROP FUNCTION IF EXISTS db.GetUserIDByCourseID;
CREATE FUNCTION db.`GetUserIDByCourseID`(CourseID int) RETURNS int(11)
BEGIN
      SELECT (c.user_id + COALESCE(pi.user_id, 0) + COALESCE(p.user_id, 0))
        INTO @user_id
        FROM courses c
             LEFT JOIN users u
                ON u.user_id = c.user_id
             LEFT JOIN partners p
                ON p.partner_id = c.partner_id
             LEFT JOIN partners_individual pi
                ON pi.individual_id = c.individual_id;
             WHERE c.course_id = CourseID;

      SELECT user_type_id
        INTO @user_type_id
        FROM users
       WHERE user_id = @user_id;

      RETURN @user_type_id;
   END;

I keep getting this error:
Result consisted of more than one row

I have this function:

DROP FUNCTION IF EXISTS db.GetUserIDByCourseID;
CREATE FUNCTION db.`GetUserIDByCourseID`(CourseID int) RETURNS int(11)
BEGIN
      SELECT (c.user_id + COALESCE(pi.user_id, 0) + COALESCE(p.user_id, 0))
        INTO @user_id
        FROM courses c
             LEFT JOIN users u
                ON u.user_id = c.user_id
             LEFT JOIN partners p
                ON p.partner_id = c.partner_id
             LEFT JOIN partners_individual pi
                ON pi.individual_id = c.individual_id;
             WHERE c.course_id = CourseID;

      SELECT user_type_id
        INTO @user_type_id
        FROM users
       WHERE user_id = @user_id;

      RETURN @user_type_id;
   END;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和影子一齐双人舞 2024-10-01 00:03:42

SELECT进入变量时,结果集必须恰好由一行组成。在您当前的示例中,第一个 select 语句可能会返回多行;您可以通过手动执行来检查出现问题的 CourseID 值。我不能说该怎么办,因为这取决于你的设计细节。

When SELECTing into a variable, the result set must consist of exactly one row. In your current example, it seems likely that the first select statement will return more than one row; you can check that by executing it by hand for the values of CourseID that are giving trouble. What to do about it I couldn't say, as that depends on the details of your design.

↘人皮目录ツ 2024-10-01 00:03:42

您可以尝试选择前 1 个。您的一个或两个 SELECT 语句可能会返回多行。我认为您需要坐下来 1:了解您的数据模型,2:了解您的数据。

为了更清楚地了解发生了什么,请单独运行这些查询并查看结果集:

SELECT (c.user_id + COALESCE(pi.user_id, 0) + COALESCE(p.user_id, 0))
  FROM courses c
  LEFT JOIN users u
      ON u.user_id = c.user_id
LEFT JOIN partners p
  ON p.partner_id = c.partner_id
LEFT JOIN partners_individual pi
  ON pi.individual_id = c.individual_id;
    WHERE c.course_id = CourseID;

SELECT user_type_id
  FROM users
  WHERE user_id = @user_id;

You could try SELECT TOP 1. Either or both your SELECT statements could be returning multiple rows. I think you need to sit down and 1: understand your data model and 2: understand your data.

To see what's going on more clearly, run these queries by themselves and look at the result sets:

SELECT (c.user_id + COALESCE(pi.user_id, 0) + COALESCE(p.user_id, 0))
  FROM courses c
  LEFT JOIN users u
      ON u.user_id = c.user_id
LEFT JOIN partners p
  ON p.partner_id = c.partner_id
LEFT JOIN partners_individual pi
  ON pi.individual_id = c.individual_id;
    WHERE c.course_id = CourseID;

SELECT user_type_id
  FROM users
  WHERE user_id = @user_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文