【问题标题】:Trying to find all books with the name Python psql尝试查找名称为 Python psql 的所有书籍
【发布时间】:2022-01-10 10:00:06
【问题描述】:

您好,我正在为我的软件工程课做 psql 数据库的作业,我必须做的一个问题是“查找所有包含 Python 的书籍”,所以我尝试使用 WHERE 语法,但它会给出我 0 行没有标题中包含 Python 一词的书籍,但显然书籍表中有任何人可以帮我解决这个问题吗?

booktown=# SELECT books.title FROM books WHERE title NOT IN ('Python');

            title
-----------------------------
 The Shining
 Dune
 2001: A Space Odyssey
 The Cat in the Hat
 Bartholomew and the Oobleck
 Franklin in the Dark
 Goodnight Moon
 Little Women
 The Velveteen Rabbit
 Dynamic Anatomy
 The Tell-Tale Heart
 Programming Python
 Learning Python
 Perl Cookbook
 Practical PostgreSQL
(15 rows)

booktown=# SELECT books.title FROM books WHERE title IN ('Python');
 title
-------
(0 rows)

booktown=# SELECT books.title FROM books WHERE title LIKE 'Python';
 title
-------
(0 rows)

booktown=# SELECT books.title FROM books WHERE title LIKE 'Python';

【问题讨论】:

标签: sql postgresql sql-like


【解决方案1】:

您应该使用 Postgresql 的 LIKE 运算符进行模式匹配。您查询的方式将进行精确匹配。请参阅https://www.postgresqltutorial.com/postgresql-like/ 上的示例

【讨论】:

    【解决方案2】:

    title IN ('Python') 查找标题在列表中的所有书籍。该列表仅包含一个条目“Python”。没有标题为“Python”的书。

    title LIKE 'Python' 查找标题与模式“Python”匹配的所有书籍。由于模式中没有通配符(即%_),因此会查找完全匹配。同样,没有名为“Python”的书。

    您希望所有标题都匹配“零个或多个我们不关心的字符,然后是单词 Python,然后是零个或多个我们不感兴趣的字符”的模式。

    那是title LIKE '%Python%'

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 2012-01-14
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 2021-12-20
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多