DEV Community

panfan
panfan

Posted on

Postgresql - Select

示例数据库

使用数据库时,最常见的就是使用SELECT语句来查询数据。

SELECT语句是 Postgresql 最复杂的一种查询语句,它可以查询数据库中的所有数据,也可以查询指定的数据。

  • DISTINCT查询不同行的数据
  • ORDER BY排序查询结果
  • WHERE条件过滤查询结果
  • LIMITFETCH限制查询结果的数量
  • GROUP BY分组查询结果
  • HAVING条件过滤分组查询结果
  • INNER JOIN内连接查询结果
  • LEFT JOIN左连接查询结果
  • FULL OUTER JOIN全连接查询结果
  • CROSS JOIN交叉连接查询结果
  • UNION查询结果的并集
  • INTERSECT查询结果的交集
  • EXCEPT查询结果的差集

SELECT 语法

SELECT [select_list] FROM [table_name]

  • select_list:指定一个或多个查询字段,也可以使用*来查询所有字段
  • table_name:指定查询的表名

SELECT 语句的示例

查询所有客户的名字

SELECT first_name FROM customer;
Enter fullscreen mode Exit fullscreen mode

OZfMMd

;表示语句结束

查询多个字段

SELECT first_name, last_name, email FROM customer;
Enter fullscreen mode Exit fullscreen mode

6Snfin

查询所有数据

SELECT * FROM customer;
Enter fullscreen mode Exit fullscreen mode

尽量避免使用*,因为它会查询所有字段,会影响性能。

表达式查询

使用||连接姓名查询

SELECT first_name || ' ' || last_name, email FROM customer;
Enter fullscreen mode Exit fullscreen mode

UbI7CW

SELECT 5*3;
Enter fullscreen mode Exit fullscreen mode

eCZOtM

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up