Sql 查询语法错误

Sql 查询语法错误,sql,case,Sql,Case,我对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 W

我对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

或者使用单引号,比如

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 = '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;"
或者(假设您的客户机支持)使用bind参数,如

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;"

寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:突出显示的语法应该会泄露这一点。可能
ROLE\u USER
1:st查询周围未替换的双引号是有效的ANSI SQL语法。UID是特定于产品的保留字。双引号“uId”是安全的。2:nd查询包含特定于产品的IIF函数。但问题是使用了围绕角色的双引号。双引号用于对象名称(例如列)。字符串文字有单引号,例如“ROLE\u USED”。