question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do write a subquery?

See original GitHub issue

Hi. @tgriesser . first of all congratulations on the superb job you all have been doing creating this tool. It looks amazing and it’s working quite well.

for example (for postgres)

SELECT
   *
 FROM
   tbl_a LEFT join (
      SELECT 
          id, name 
      FROM 
           tbl_b
      GROUP BY 
           id, name
   )  AS tbl_b on tbl_a.id = tbl_b.id

now

Knex('tbl_a').select('*').join(
                Knex.Raw('(SELECT id, name FROM tbl_b GROUP BY id, name) AS tbl_b')
                ,'tbl_a.id', '=', 'tbl_b.id', 'LEFT');

I want to create without the Raw method.

Is it possible to other, using the USING?

thank you.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

30reactions
ivan-kleshnincommented, Sep 3, 2015

It is possible:

// [users] to [offers] :: [1] to [n]

let countedVisibleOffers = knex.select("user_id")
  .count("* as total")
  .from("offers")
  .groupBy("user_id")
  .as("visible_offers");

console.log(
  knex.select("*", "visible_offers.total")
    .from("users")
    .leftOuterJoin(countedVisibleOffers, "visible_offers.user_id", "users.id")
    .where(...)
    .orderBy(...)
    .toString()
);
9reactions
tgriessercommented, Jul 30, 2013

Hi @dai-yamashita - glad you’re liking it!

Currently there isn’t support for doing sub-queries for joins, and the Knex.Raw would be the best way to go about it - though there’s no reason that couldn’t be allowed - I’ll look into adding a way to do it using an independent query:

Would this be what you’re looking for?

Knex('tbl_a').select('*').join(
   Knex('tbl_b').select('id', 'name').groupBy('id', 'name'),
   'tbl_a.id', 
   '=', 
   'tbl_b.id', 
   'LEFT'
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing Subqueries in SQL | Advanced SQL - Mode Analytics
Subqueries (also known as inner queries or nested queries) are a tool for performing operations in multiple steps. For example, if you wanted...
Read more >
SQL Subqueries - w3resource
A subquery is a SQL query nested inside a larger query. A subquery may occur in : ... You can use a subquery...
Read more >
SQL - Sub Queries - Tutorialspoint
SQL - Sub Queries ; SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM CUSTOMERS WHERE SALARY > ; UPDATE CUSTOMERS SET...
Read more >
5 SQL Subquery Examples | LearnSQL.com
A subquery, or nested query, is a query placed within another SQL query. When requesting information from a database, you may find it...
Read more >
SQL Subquery (With Examples) - Programiz
SQL Subquery ; * FROM Customers WHERE age = ( ; customer_id, first_name FROM Customers WHERE customer_id ; DISTINCT Customers.customer_id, Customers.first_name ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found