[파이썬] Peewee Subquery 작성

In this blog post, we will explore how to use subqueries in Peewee, a lightweight ORM (Object Relational Mapper) for Python.

What is a Subquery?

A subquery, also known as a nested query or inner query, is a query embedded within another query. It allows us to retrieve data from one table and use it as a condition or filter in another query.

Why use Subqueries?

Subqueries enable us to write complex queries in a more concise and efficient manner. They can be used to perform calculations using aggregated data, filter results based on subquery conditions, or retrieve data from related tables.

Using Subqueries in Peewee

Peewee provides a convenient way to write subqueries using its expressive API. Let’s look at some examples of how to use subqueries in Peewee.

Example 1: Subquery as a Filter Condition

from peewee import *

database = SqliteDatabase('my_database.db')

class User(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = database

class Post(Model):
    user = ForeignKeyField(User)
    content = TextField()

    class Meta:
        database = database

database.connect()

# Subquery to retrieve all users younger than 25
subquery = User.select().where(User.age < 25)

# Query to retrieve posts made by users younger than 25
query = Post.select().where(Post.user << subquery)

for post in query:
    print(f"Post: {post.content}, User: {post.user.name}")

In the example above, we defined two models, User and Post, which are related through a foreign key. We then used a subquery to retrieve all users younger than 25 and used that subquery as a filter condition in another query to retrieve the posts made by those users.

Example 2: Subquery with Aggregation

from peewee import *

database = SqliteDatabase('my_database.db')

class User(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = database

class Post(Model):
    user = ForeignKeyField(User)
    content = TextField()
    likes = IntegerField()

    class Meta:
        database = database

database.connect()

# Subquery to calculate the average number of likes per user
subquery = Post.select(fn.AVG(Post.likes)).group_by(Post.user)

# Query to find users whose average number of likes is greater than 100
query = User.select().where(User.id.in_(subquery) & (subquery > 100))

for user in query:
    print(f"User: {user.name}")

In this example, we used a subquery with aggregation to calculate the average number of likes per user. We then used that subquery to filter the users whose average number of likes is greater than 100.

Conclusion

Subqueries are powerful tools in SQL that can greatly enhance the flexibility and efficiency of your queries. With Peewee’s intuitive API, writing subqueries becomes even easier. Whether you need to filter results based on subquery conditions or perform calculations using aggregated data, Peewee provides a convenient way to achieve your desired results.

I hope this blog post has given you a good introduction to using subqueries in Peewee. Start integrating subqueries into your Peewee queries and unlock the full potential of your database interactions!