我必须使用INSERT INTO命令从数据库中获取数据然后在同一个表上我想使用update命令(这取决于通过Insert into命令插入的值)。 我想对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)
答案 0 :(得分:0)
您可以使用2个单独的查询并使用mysql
LAST_INSERT_ID()
在你的UPDATE查询
上