我对SQL很新。我只是想生成一个表并在某些条件下填写一些值。你在这个查询中看到任何语法错误吗?非常肯定错误在case语句中,因为没有case语句,表生成就好了。
q = "select tbuc.csId, tbui.role_type,
(case when tbui.role_type = "ROLE_USER" then 1 else 0 end) as
assignment_submitted FROM tb_user_click tbuc, tb_user_info tbui WHERE
tbui.user_id = tbuc.uId and tbuc.csId = tbui.class_section_id;"
也尝试使用IIF,错误信息如下:
File "1.py", line 118
q = "select tbuc.csId, tbui.role_type,IIF(role_type = "ROLE_USER", 1, 0)
as assignment_submitted FROM tb_user_click tbuc, tb_user_info tbui
WHERE tbui.user_id = tbuc.uId and tbuc.csId = tbui.class_section_id;"
SyntaxError: invalid syntax
答案 0 :(得分:1)
使用单引号,例如
q = "select tbuc.csId, tbuc.uId, tbuc.cId, tbui.role_type, tbuc.time as login_date"
+ ", tbuc.sessId, (case when tbui.role_type = 'ROLE_USER' then 1 else 0 end) "
+ "as assignment_submitted FROM tb_user_click tbuc, tb_user_info tbui WHERE "
+ "tbui.user_id = tbuc.uId and tbuc.csId = tbui.class_section_id;"
或者转义双引号,例如
q = "select tbuc.csId, tbuc.uId, tbuc.cId, tbui.role_type, tbuc.time as login_date"
+ ", tbuc.sessId, (case when tbui.role_type = \"ROLE_USER\" then 1 else 0 end"
+ ") as assignment_submitted FROM tb_user_click tbuc, tb_user_info tbui WHERE "
+ "tbui.user_id = tbuc.uId and tbuc.csId = tbui.class_section_id;"
或者(假设您的客户端支持它)使用绑定参数,如
q = "select tbuc.csId, tbuc.uId, tbuc.cId, tbui.role_type, tbuc.time as login_date"
+ ", tbuc.sessId, (case when tbui.role_type = ? then 1 else 0 end) "
+ "as assignment_submitted FROM tb_user_click tbuc, tb_user_info tbui WHERE "
+ "tbui.user_id = tbuc.uId and tbuc.csId = tbui.class_section_id;"