1
我遇到了子查詢問題。Mysql Subqery問題
我爲當地的自行車俱樂部製作了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和courseID
我建立一個存儲過程最多能夠統計出的用戶,他們的總時間等,但是我希望僅供其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' , 謝謝。