© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
piscopancer

how to replicate **joins** in **db.query**?

this is a
db.select
db.select
query with a join to filter out only answers that are accepted
const queryAcceptedAnswers = (userId: number)=>
  db.select()
  .from(answersTable)
  .where(eq(answersTable.authorId, userId))
  .innerJoin(questionsTable, eq(answersTable.id, questionsTable.acceptedAnswerId))
const queryAcceptedAnswers = (userId: number)=>
  db.select()
  .from(answersTable)
  .where(eq(answersTable.authorId, userId))
  .innerJoin(questionsTable, eq(answersTable.id, questionsTable.acceptedAnswerId))

let's say, there may be 2 accepted answers, thus this will be the response
"acceptedAnswers": [
    {
      "answers": {
        "id": 8,
        "content": "🤡🤡🤡",
        "authorId": 109352196,
        "questionId": 7,
        "createdAt": "2024-03-23T13:14:22.690Z",
        "useless": false
      },
      "questions": {
        "id": 7,
        "title": "какыфмыфвфывс",
        "content": "фывфывыфвывс",
        "authorId": 109352196,
        "createdAt": "2024-03-22T11:59:37.434Z",
        "category": "отношения",
        "acceptedAnswerId": 8
      }
    },
    {
      "answers": {
        "id": 6,
        "content": "aaa",
        "authorId": 109352196,
        "questionId": 8,
        "createdAt": "2024-03-23T11:46:38.130Z",
        "useless": false
      },
      "questions": {
        "id": 8,
        "title": "bruh",
        "content": "bruh222",
        "authorId": 109352196,
        "createdAt": "2024-03-22T11:59:37.992Z",
        "category": "отношения",
        "acceptedAnswerId": 6
      }
    }
  ]
"acceptedAnswers": [
    {
      "answers": {
        "id": 8,
        "content": "🤡🤡🤡",
        "authorId": 109352196,
        "questionId": 7,
        "createdAt": "2024-03-23T13:14:22.690Z",
        "useless": false
      },
      "questions": {
        "id": 7,
        "title": "какыфмыфвфывс",
        "content": "фывфывыфвывс",
        "authorId": 109352196,
        "createdAt": "2024-03-22T11:59:37.434Z",
        "category": "отношения",
        "acceptedAnswerId": 8
      }
    },
    {
      "answers": {
        "id": 6,
        "content": "aaa",
        "authorId": 109352196,
        "questionId": 8,
        "createdAt": "2024-03-23T11:46:38.130Z",
        "useless": false
      },
      "questions": {
        "id": 8,
        "title": "bruh",
        "content": "bruh222",
        "authorId": 109352196,
        "createdAt": "2024-03-22T11:59:37.992Z",
        "category": "отношения",
        "acceptedAnswerId": 6
      }
    }
  ]

How do I use joins in
db.query
db.query
api that will result in the same response or will filter out users accepted answers in a similar way? is it even possible with
db.query
db.query
? do i have to manage my relations in the schema?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to use nested joins with relational query builder
Drizzle TeamDTDrizzle Team / help
2y ago
How to do conditional joins with the query builder?
Drizzle TeamDTDrizzle Team / help
3y ago
How to query join table with db.query syntax?
Drizzle TeamDTDrizzle Team / help
15mo ago
use `count` in db.query?
Drizzle TeamDTDrizzle Team / help
2y ago