Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JOIN on Query #451

Closed
yankyhgoflow opened this issue Feb 19, 2021 · 3 comments
Closed

JOIN on Query #451

yankyhgoflow opened this issue Feb 19, 2021 · 3 comments

Comments

@yankyhgoflow
Copy link
Contributor

I was wondering wether there is a way to JOIN on a Query which just defines a FROM (no SELECTs etc.).

i.e.

Here is the sample SQL I am aiming to get

SELECT
  *
FROM
  "Sales"
  INNER JOIN "Items" ON ("Sales"."Item" = "Items"."Item");

However, my code is equivalent to

var query = new Query("Sales")
    .Join(new Query("Items"), join => join.On("Sales.Item", "Items.Item"));

which results in the following SQL

SELECT
  *
FROM
  "Sales"
  INNER JOIN (
    SELECT
      *
    FROM
      "Items"
  ) ON ("Sales"."Item" = "Items"."Item");

my code considers all tables as Querys which only works for the "Sales" table (as that is the initial Query), passing a Query containing only a table name results in the Join JOINing on a subquery of the table passed...

@ahmad-moussawi
Copy link
Contributor

You can simply pass the table name as string,

var query = new Query("Sales")
    .Join("Items", j => j.On("Sales.Item", "Items.Item"));

Or even simpler without using the join lambda

var query = new Query("Sales")
    .Join("Items", "Sales.Item", "Items.Item");

since the join condition is simple columns equality condition.

Here is a live example https://sqlkata.com/playground?code=var%20query%20%3D%20new%20Query(%22Sales%22)%0A%20%20%20%20.Join(%22Items%22%2C%20j%20%3D%3E%20j.On(%22Sales.Item%22%2C%20%22Items.Item%22))%3B

@yankyhgoflow
Copy link
Contributor Author

Thanks for the response.

In reality I should have started my previous post with a big thanks to you, SqlKata is really a great tool and I really appreciate the work you did and that you open sourced it!

To better phrase my question, I am using SqlKata within my project and I am wrapping my objects (whether EF models etc.) in there respective types, hence I made all my "tables" create a Query which works in the case of a single table query and it works in a JOIN query for the table on the LHS.

This is not really an issue as It might 100% be that I need to rethink the design of my little project, but I was more wondering whether there is a way in SqlKata to do it.

Once again thank you for this great tool!

@yankyhgoflow
Copy link
Contributor Author

Reopening this issue as a PR was created to help me solve my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants