我做了具體需要的SQL查詢的,但我不是一個專業的SQL的,所以我需要幫助「改進」查詢。優化SQL查詢更新取決於另一個表
首先,我有3個表:
- 用戶(ID,用戶名,...)
- 行動(ID,點,limitPerDay)
- 歷史(ID,USER_ID,的action_id,created_at ,is_used_for_score)
每次用戶做特定的動作,它是日誌中記錄表(如果已達到限制或不is_used_for_score將是true或false)
但我需要,因爲某些條件下,查詢更新所有is_used_for_score is_used_for_score可以從虛假地傳遞到真正
我的查詢實際上是
SET @num := 0, @user_id := 0, @type := '', @date := '';
UPDATE history SET is_used_for_score = 1 WHERE history.id IN (
SELECT history_id FROM (
SELECT
history.id AS history_id,
action.limitPerDay AS limit_per_day,
action.point,
@num := IF(
@type = action_id,
IF(
@date = CONCAT(YEAR(history.created_at), '-', MONTH(history.created_at), '-', DAY(history.created_at)),
IF (
@user_id = user_id,
@num + 1,
1
),
1
),
1
) AS row_number,
@user_id := user_id AS user_id,
@type := action_id AS action_id,
@date := CONCAT(YEAR(history.created_at), '-', MONTH(history.created_at), '-', DAY(history.created_at)) AS `date`
FROM history
LEFT JOIN `action` ON action.id = action_id
HAVING (row_number <= limit_per_day) OR (limit_per_day IS NULL)
ORDER BY history.created_at ASC, user_id, action_id
) AS history_id_count_for_vote
);
但是,我敢肯定的是可能不是最好的辦法要做到這一點。你有什麼建議可以改善查詢嗎?
謝謝
有沒有辦法,我通過涉水。無論如何,祝你好運。 – Strawberry
用英文描述需要計算'is_used_for_score'的情況。 –