【问题标题】:Postgres SELECT* FROM table WHERE column-varchar=="string-example"?Postgres SELECT* FROM table WHERE column-varchar=="string-example"?
【发布时间】:2016-06-16 20:36:48
【问题描述】:

我有下表:

CREATE TABLE lawyer (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL UNIQUE,
  name_url VARCHAR check(translate(name_url, 'abcdefghijklmnopqrstuvwxyz-', '') = '') NOT NULL UNIQUE
);

我想SELECT * FROM lawyer where name_url = "john-doe"

【问题讨论】:

  • 你可以使用SELECT * FROM lawyer where name_url = 'john-doe',这和你写的差不多,但是在你的字符串文字周围加上单引号。
  • 那么你的问题是什么?不要对字符串文字使用双引号,像'john-doe' 那样使用单引号。此外,使用varchar 而不指定列宽是一种不好的做法。

标签: postgresql postgresql-9.1 postgresql-9.3


【解决方案1】:

字符文字放在单引号中:

SELECT * 
FROM lawyer 
where name_url = 'john-doe';

详见说明书:
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS

【讨论】:

    【解决方案2】:

    寻找完全匹配:

    select column1, column2 from mytable where column2 like 'string';
    

    模式匹配可以使用:(returns stringxx, stringyyy..)

    select column1, column2 from mytable where column2 like 'string%';
    

    这将返回任何与 string*** 匹配的 column2。

    对于使用OR条件,可以使用以下多个字符串进行比较:

    select column1, column2 from mytable where column2 ~* 'string1|string2';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多