pandas.read\u sql\u query-如果列名和搜索值都包含空格,如何正确写入where条件

1cklez4t  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(329)

我这样编写sql查询

df = pd.read_sql_query('SELECT * FROM hr_dataset WHERE "Performance Score" = "Needs Improvement";', conn)
hr_dataset - table
Performance Score - column
Needs Improvement - searched value

列名和搜索值都包含空格,这就是为什么我必须使用引号,但是这个查询结果有一个错误。


**DatabaseError**: Execution failed on sql 'SELECT * FROM hr_dataset WHERE "Performance Score" = "Needs Improvement";': column "Needs Improvement" does not exist

LINE 1: ...ECT * FROM hr_dataset WHERE "Performance Score" = "Needs Imp...

我应该如何正确地编写这样的查询?

izj3ouym

izj3ouym1#

请使用下面的查询,“需要改进”用双引号括起来,数据库将其视为列名

SELECT * FROM hr_dataset WHERE Performance Score = 'Needs Improvement';
eni9jsuy

eni9jsuy2#

如果是sql server

SELECT * FROM hr_dataset WHERE [Performance Score] like '%Needs Improvement%'

如果是mysql

SELECT * FROM hr_dataset WHERE `Performance Score` like '%Needs Improvement%'

如果是postgresql,请尝试

SELECT * FROM hr_dataset WHERE "Performance Score" like '%Needs Improvement%'

请参阅postgressql的小提琴

相关问题