1

我遇到了子查询的问题。

我正在为我当地的自行车俱乐部争取最好的 10 次计时赛联赛。有一个每周联赛,进入的人将他们的时间和他们的时间一起记录在一张桌子上。

这是我的存储过程。

BEGIN
    #Routine body goes here...
        DROP TEMPORARY TABLE IF EXISTS tbl_Temp;

        CREATE TEMPORARY TABLE tbl_Temp (
        UserId int DEFAULT 0,
        TotalTime DECIMAL (10,2),
        EventCount int DEFAULT 0,
        FullName VARCHAR(255),
        Marshalled TINYINT(2) DEFAULT 0,
        HillClimb TINYINT(2) DEFAULT 0,
        Year INT(10)
        );

        INSERT INTO tbl_Temp (UserID,Year)
        SELECT DISTINCT userId,YEAR(date)
        FROM tbl_series;

        /* ---- EVENT COUNT */
        UPDATE tbl_Temp AS c,tbl_series AS t
        SET c.EventCount = (SELECT COUNT(userId) FROM tbl_series WHERE (UserID = t.userId))
        WHERE c.UserID = t.userId;

        /* ---- get the times into a temp table */


        /* ---- TOTAL TIME */
        UPDATE tbl_Temp AS c,tbl_series AS t
        SET c.TotalTime = (
            /*SELECT SUM(A.Time) FROM (
                    SELECT Time FROM tbl_series
                    WHERE (UserID = t.userId) 
                    LIMIT 10
                ) AS A*/
                SELECT SUM(Time) FROM tbl_series 
                WHERE (userId = t.UserId)
        )
        WHERE c.UserID = t.userId;

        /* ---- Name */
        UPDATE tbl_Temp AS c,tbl_users AS t
        SET c.FullName = (
                SELECT NAME
                FROM tbl_users
                WHERE (userId = t.UserId)
        ) 
        WHERE (c.userId = t.UserId);

        /* ---- Marshalled */
        /*  will need to add in a new table for marshalled so isn't lost over the coming years */
        UPDATE tbl_Temp AS c,tbl_users AS t
        SET c.Marshalled = (
                SELECT marshalled
                FROM tbl_users
                WHERE (userId = t.UserId)
        ) 
        WHERE (c.userId = t.UserId);

        /* ---- Hill Climb */
        UPDATE tbl_Temp AS c,tbl_users AS t
        SET c.HillClimb = (

            SELECT Count(tbl_series.SeriesID)
            FROM tbl_series
            INNER JOIN tbl_series_courses ON tbl_series.CourseID = tbl_series_courses.SeriesCourseID AND tbl_series.CourseID = tbl_series_courses.SeriesCourseID
            where (EventType = 2) AND (userId = t.UserId)

        ) 
        WHERE (c.userId = t.UserId);

        /* ---- select all */
        SELECT * FROM tbl_Temp

        ORDER BY EventCount Desc,TotalTime;

END

tbl_series 存储时间、用户 ID 和课程 ID

我正在构建一个存储过程,它可以计算出用户、他们的总时间等,但是我只希望他们的前 10 个最快时间,但 MySql 对此有限制。我无法运行以下代码。

/* ---- TOTAL TIME */
        UPDATE tbl_Temp AS c,tbl_series AS t
        SET c.TotalTime = (
            SELECT SUM(A.Time) FROM (
                    SELECT Time FROM tbl_series
                    WHERE (UserID = t.userId) 
                    LIMIT 10
                ) AS A
        )
        WHERE c.UserID = t.userId;

我得到了错误

程序执行失败

1054 - 'where 子句'中的未知列't.userId'

任何想法都会很棒,谢谢。

4

1 回答 1

0

啊……简单,MySql 不支持LIMIT内部查询。
打开游标,在主结果集上循环,并为循环中的每条记录获取所需的数据。

于 2012-05-07T12:57:26.917 回答