Simple Postgresql Statement - column name does not exists

孤街醉人 提交于 2019-12-17 09:58:51

问题


I've been pulling my hair out. I have a very simple postgre database, one specific table has a column named lName (uppercase N). Now I know with postgre I must quote lName since it contains an uppercase N.

I am trying to query the database with the following statement:

SELECT * 
FROM employee 
WHERE "lName" LIKE "Smith"

But I am receive this error:

Warning: pg_query() [function.pg-query]: Query failed: ERROR: column "Smith" does not exist in .....

What is the issue here? Why is it saying the column is "Smith"?


回答1:


I would guess:

 SELECT * FROM employee WHERE "lName" LIKE 'Smith'

(note the different quotes; "foo" is a quoted identifier; 'foo' is a string literal)

Also, in most SQL dialects, a LIKE without a wildcard is equivalent to =; did you mean to include a wildcard?




回答2:


Because "Smith" is an identifier, and in that position, an identifier is expected to be a column. What you probably meant is a string literal, which uses single quotes: 'Smith'. So

SELECT * FROM employee WHERE "lName" LIKE 'Smith'

You probably also want a wildcard in the string to search for ('Smith%'?). LIKE matching is anchored to the beginning and end of a string, unlike typical regular expression matching.



来源:https://stackoverflow.com/questions/5800230/simple-postgresql-statement-column-name-does-not-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!