查询查询 Unknown column ‘xxx’ in ‘where clause

在sql查询中,由于不规范的操作会导致Unknown column ‘xxx’ in ‘where clauseolumn ‘xxx’ in ‘where clause错误。
产生的原因是sql语句 拼接不规范导致。查询字段是int型时,不需要添加引号, 当查询字段为字符串型时,则需要添加引号。
正确的语句:

 sql = 'select\
        case when jdbz ="出"  then cxkh  else jydfzkh end cxkh,\
        case when jdbz ="出"  then jymc  else jydfmc end jymc,\
        jysj,jyje,\
        case when jdbz ="出"  then jydfzkh  else cxkh end jydfzkh,\
        case when jdbz ="出"  then jydfmc  else jymc end jydfmc\
        from df where jymc ="'+jymc+'")t1'

错误的语句:

sql = 'select\
        case when jdbz ="出"  then cxkh  else jydfzkh end cxkh,\
        case when jdbz ="出"  then jymc  else jydfmc end jymc,\
        jysj,jyje,\
        case when jdbz ="出"  then jydfzkh  else cxkh end jydfzkh,\
        case when jdbz ="出"  then jydfmc  else jymc end jydfmc\
        from df where jymc ='+jymc+')t1'

你可能感兴趣的:(查询查询 Unknown column ‘xxx’ in ‘where clause)