0
我必須使用INSERT INTO命令從數據庫中獲取數據,然後在同一張表上使用update命令(這取決於通過插入命令插入的值)。 我想使用一個單一的查詢插入和更新命令?如何在一個mysql查詢中一起使用INSERT INTO和UPDATE?
你可以參考我的命令
INSERT INTO `cr_score_table`(`User_Id`,`CR_Id`, `Question_attempted`, `Score`)
SELECT wp_wp_pro_quiz_statistic_ref.user_id,wp_wp_pro_quiz_statistic.statistic_ref_id, SUM(wp_wp_pro_quiz_statistic.solved_count), SUM(wp_wp_pro_quiz_statistic.correct_count)
FROM wordpress.wp_wp_pro_quiz_statistic_ref
LEFT JOIN wordpress.wp_wp_pro_quiz_statistic
ON wp_wp_pro_quiz_statistic_ref.statistic_ref_id = wp_wp_pro_quiz_statistic.statistic_ref_id
Group By statistic_ref_id
ON DUPLICATE KEY UPDATE cr_score_table.CR_Id=cr_score_table.CR_Id
和
UPDATE cr_score_table AS X JOIN (
SELECT
s1.CR_Id, COUNT(s2.CR_Id)/ (SELECT COUNT(*) FROM cr_score_table) * 100 AS percentile
FROM
cr_score_table s1 JOIN cr_score_table s2 on (s2.score <= s1.score)
GROUP BY s1.Cr_Id
ORDER BY s1.Cr_Id) AS Z
ON (X.Cr_Id = Z.Cr_Id)
SET X.Score_Percentile = Z.percentile, `Name`= (Select Name
From user_table
WHERE user_table.User_Id = X.User_Id)
我沒有得到你......我想寫這兩個函數在一個查詢中。您可以使用我寫的查詢爲我寫一個查詢 –