我有一个查询来更新数据(如果存在)或插入新记录。该查询也在for循环中运行:
$pname = $c_info['name'];
$cname = $model[$c_id]['name'];
$iqs =" IF EXISTS (SELECT * FROM custom_question_details WHERE Quiz_id
=$data[$i] and User_id=$user_id and Parent_Category='$pname' and
Category_Name='$cname' and Score=$temp )
UPDATE custom_question_details SET Quiz_id =$data[$i],User_id=$user_id,
Parent_Category='$pname', Category_Name='$cname',Score=$temp
ELSE
insert into custom_question_details (User_id,Quiz_id,Parent_Category,Category_Name,Score)
values (".$user_id.",".$data[$i].",'".$c_info['name']."','".$model[$c_id]['name']."','".$temp."')";
echo "<br>".$iqs;
我通过在PHP脚本中执行某些计算来获取变量值。
这些是查询的一些错误。在回显上面的sql查询时,也会打印这些数据。
IF EXISTS (
SELECT *
FROM custom_question_details
WHERE Quiz_id =7
AND User_id =3
AND Parent_Category = 'Default for System'
AND Category_Name = 'sample quiz question 1'
)UPDATE custom_question_details SET Quiz_id =7,User_id=3,Parent_Category='Default for System',Category_Name='sample quiz question 1',Score='100'
ELSE insert into custom_question_details (User_id,Quiz_id,Parent_Category,Category_Name,Score) values(3,7,'Default for System','sample quiz question 1','100')
为什么我的查询错了?我甚至尝试过:
SELECT EXISTS (
SELECT *
FROM custom_question_details
WHERE Quiz_id =7
AND User_id =3
AND Parent_Category = 'Default for System'
AND Category_Name = 'sample quiz question 1'
)UPDATE custom_question_details SET Quiz_id =7,User_id=3,
Parent_Category='Default for System',Category_Name='sample quiz question 1',Score='100'
ELSE
insert into custom_question_details (User_id,Quiz_id,Parent_Category,Category_Name,Score) values(3,7,'Default for System','sample quiz question 1','100')
......但它仍然无效。我哪里错了?
答案 0 :(得分:0)
首先,IF条件语法是这样的:
IF EXISTS ([condition]) THEN [your code] ELSE [some other code]
其次,我认为你缺少UPDATE方法的一些括号。 第三,我不确定你是否可以在IF条件下使用多个列。我需要多研究一下。
也许您可以告诉我们您收到的确切错误?!
希望它有所帮助, 丹尼斯雷德勒