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.

[Question] How one could implement JOIN with a subquery?

See original GitHub issue

First of all, thank you for your hard work. The question is, how to write a query like below using Exposed

SELECT *
FROM categories
      LEFT JOIN products ON products.id = (
             SELECT products.id 
             FROM products 
             WHERE products.category_id = categories.id 
             LIMIT 1
     )
LIMIT 10

I’d found related issue but it didn’t help

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Tapaccommented, Apr 1, 2021

There is eqSubQuery operator in Exposed, you can use it like:

Categories.join(Products, JoinType.LEFT) {
   Products.id eqSubQuery Products.slice(Products.id).select { Products.id eq Categories.id).limit(1)
}

But I think in that case it’s more efficient to not use subquery. afaiu the code below will give same result.

SELECT * 
FROM categories 
   LEFT JOIN products on categories.id = products.category_id
LIMIT 10
0reactions
Tapaccommented, Apr 3, 2021

Please provide raw SQL and I’ll try to show how to convert it into Exposed DSL

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subquery vs. JOIN - LearnSQL.com
Subqueries and JOIN s can both be used in a complex query to select data from multiple tables, but they do so in...
Read more >
Subqueries and JOINs - MariaDB Knowledge Base
Rewriting Subqueries as JOINS​​ A subquery using IN can be rewritten with the DISTINCT keyword, for example: SELECT * FROM table1 WHERE col1...
Read more >
SQL Join vs Subquery - GeeksforGeeks
What are Joins? A join is a query that combines records from two or more tables. A join will be performed whenever multiple...
Read more >
Joins and Subqueries in SQL - Web Age Solutions
An SQL Join statement is used to combine data or rows from two or more tables based on a common field between them....
Read more >
Understanding when to use a subquery over a join
They're not the same thing. In the query with joins you can potentially multiply rows or have rows entirely removed from the results....
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