📜  Python PostgreSQL-Where子句(1)

📅  最后修改于: 2023-12-03 15:34:03.559000             🧑  作者: Mango

Python PostgreSQL - WHERE子句

在 PostgreSQL 中,WHERE 子句用于指定一个条件来从表中选择数据。这个条件可以使用比较运算符、逻辑运算符和其他条件运算符来创建。

示例

假设我们有一个名为 customers 的表,拥有以下数据:

| id | name | age | address | salary | |----|------|-----|---------|--------| | 1 | Alice| 25 | New York| 50000 | | 2 | Bob | 30 | L.A. | 45000 | | 3 | Jack | 35 | Chicago | 55000 | | 4 | Jill | 40 | Boston | 60000 |

可以使用以下代码来选择名字为 "Bob" 的客户:

import psycopg2

try:
    connection = psycopg2.connect(user="username",
                                  password="password",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="database_name")

    cursor = connection.cursor()
    postgreSQL_select_Query = "select * from customers where name = 'Bob'"

    cursor.execute(postgreSQL_select_Query)
    print("Selecting rows from customers using cursor.fetchall")
    customer_records = cursor.fetchall()

    for row in customer_records:
        print("Id = ", row[0], )
        print("Name = ", row[1])
        print("Age  = ", row[2])
        print("Address  = ", row[3])
        print("Salary  = ", row[4], "\n")

except (Exception, psycopg2.Error) as error:
    print("Error while fetching data from PostgreSQL", error)

finally:
    # Closing database connection.
    if connection:
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")

这个代码会输出名字为 Bob 的客户的以下信息:

Id =  2
Name =  Bob
Age  =  30
Address  =  L.A.
Salary  =  45000
WHERE 子句参考

下面是一些可能用到的条件运算符:

| 运算符 | 描述 | 示例 | |---------|-----------------------------------------------------------|--------------------------------------------------------------| | = | 如果两个值相等,则条件成立。 | WHERE name = 'Bob' | | <> | 如果两个值不同,则条件成立。 | WHERE name <> 'Bob' | | > | 如果左侧的值大于右侧的值,则条件成立。 | WHERE age > 30 | | < | 如果左侧的值小于右侧的值,则条件成立。 | WHERE age < 30 | | >= | 如果左侧的值大于或等于右侧的值,则条件成立。 | WHERE age >= 30 | | <= | 如果左侧的值小于或等于右侧的值,则条件成立。 | WHERE age <= 30 | | BETWEEN | 如果左侧的值在右侧的两个值之间(包括它们本身),则条件成立。| WHERE age BETWEEN 30 AND 40 | | IN | 如果左侧的值在右侧的列表中,则条件成立。 | WHERE name IN ('Bob', 'Alice') | | LIKE | 如果左侧的值与右侧的模式匹配,则条件成立。 | WHERE name LIKE 'B%' |

可以同时使用多个条件,并使用 AND 和 OR 进行逻辑连接,例如:

postgreSQL_select_Query = "select * from customers where age >= 30 and salary >= 50000"

这个代码会选择年龄大于等于30且薪资大于等于50000的员工。

结论

在 PostgreSQL 中,WHERE 子句是查询语句的重要组成部分。通过使用 WHERE 子句,我们可以选择满足特定条件的数据,并从表中筛选出我们需要的信息。同时,我们可以使用多个条件和逻辑运算符,进一步指定查询条件,从而更精确地选择数据。

# 代码片段 
import psycopg2

try:
    connection = psycopg2.connect(user="username",
                                  password="password",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="database_name")

    cursor = connection.cursor()
    postgreSQL_select_Query = "select * from customers where name = 'Bob'"

    cursor.execute(postgreSQL_select_Query)
    print("Selecting rows from customers using cursor.fetchall")
    customer_records = cursor.fetchall()

    for row in customer_records:
        print("Id = ", row[0], )
        print("Name = ", row[1])
        print("Age  = ", row[2])
        print("Address  = ", row[3])
        print("Salary  = ", row[4], "\n")

except (Exception, psycopg2.Error) as error:
    print("Error while fetching data from PostgreSQL", error)

finally:
    # Closing database connection.
    if connection:
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")