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

Select from subquery #2593

Closed
dariusmb opened this issue Jul 27, 2018 · 7 comments
Closed

Select from subquery #2593

dariusmb opened this issue Jul 27, 2018 · 7 comments
Labels

Comments

@dariusmb
Copy link

dariusmb commented Jul 27, 2018

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:
How can I make the following select
SELECT * FROM post_image JOIN (SELECT * FROM post LIMIT 2) as posts_shown ON post_image.post_id = posts_shown.id;

@pleerock
Copy link
Member

Checkout this example on advanced query builder usage.

@skylight-ity
Copy link

still no response. How would you wring something like this in typeorm
select * from (select price,name,brand,market_name,image,type,category,price,give_amount,date,suggested_price,floor,exterior,float,by, RANK() OVER(ORDER BY price) from inventory) as s;

@skylight-ity
Copy link

@pleerock Chihel menavesem aka?

@chriszrc
Copy link

@skylight-ity Subqueries have been added to the documentation there:

https://typeorm.io/#/select-query-builder/using-subqueries

You would do it like:

const inventory = await connection
    .createQueryBuilder()
    .select("i.*")
    .from(subQuery => {
        return subQuery
            .select("inventory.*")
            //This works for aggregate operators like sum(), I'm guessing this should work for rank()
            .addSelect("Rank() over(order by price)", "rnk")
            .from(Inventory, "inventory")
    }, "i")
    .getRawMany();

@serg06
Copy link

serg06 commented Mar 28, 2022

@chriszrc Thank you!

Is there any way to do the same thing for .update()?

The best I was able to find was injecting the .getQuery() into .update.

@chriszrc
Copy link

@serg06 No idea, sorry. I would probably do a raw sql query in that case-

@weilinzung
Copy link

weilinzung commented Jun 22, 2023

@skylight-ity Subqueries have been added to the documentation there:

https://typeorm.io/#/select-query-builder/using-subqueries

You would do it like:

How to subselect multiple fields? 'Operand should contain 1 column(s)'

const posts = await dataSource
    .createQueryBuilder()
    .select("post.id", "id")
    .addSelect((subQuery) => {
        return subQuery.select("user.name", "name").addSelect("user.id", "id").from(User, "user")
    }, "name")
    .from(Post, "post")
    .getRawMany()

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

No branches or pull requests

7 participants