typeorm查询多托马尼关系

发布于 2025-01-31 20:01:39 字数 1133 浏览 1 评论 0原文

我正在尝试查询桌子产品以查找某些具有“类别“食物”,“冷”的产品。

我希望有人可以给我一个人,以便查询两个或更多类别 我能够使它充满活力,但我在QueryBuilder上挣扎

@Entity()
export class Product{
    @Property()
    @ManyToMany(() => Category, category=> category.products)
    @JoinTable({
        name: "product_category",
        joinColumn: {
            name: "product",
            referencedColumnName: "Id"
        },
        inverseJoinColumn: {
            name: "category",
            referencedColumnName: "Id"
        }
    })
    categories: Category[];
    }

@Entity()
export class Category {
    @Property()
    @PrimaryGeneratedColumn("uuid")
    Id: string;

    @Column()
    @Property()
    name: string;

    @Property()
    @ManyToMany(() => LibraryItems, libraryItem => libraryItem.categories)
    products: Product[];
}

我尝试使用此信息: query.Where(“ categories.name in(:... name1)和categories.name in(:... name2)”,{name1:“ food”,name2:“ cold”})

I'm trying to query over the table products to find, Certain products that have "at least" Category "Food", "Cold".

I hope someone can give me a hand to be able to query for 2 or more Categories
I'm able to make it dynamic but I'm struggling hard on the queryBuilder

@Entity()
export class Product{
    @Property()
    @ManyToMany(() => Category, category=> category.products)
    @JoinTable({
        name: "product_category",
        joinColumn: {
            name: "product",
            referencedColumnName: "Id"
        },
        inverseJoinColumn: {
            name: "category",
            referencedColumnName: "Id"
        }
    })
    categories: Category[];
    }

@Entity()
export class Category {
    @Property()
    @PrimaryGeneratedColumn("uuid")
    Id: string;

    @Column()
    @Property()
    name: string;

    @Property()
    @ManyToMany(() => LibraryItems, libraryItem => libraryItem.categories)
    products: Product[];
}

I have tried using this:
query.where("categories.name IN(:...name1) AND categories.name IN(:...name2)", { name1: "Food", name2: "Cold" })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2025-02-07 20:01:39

我们讨论。这是查询的一个示例将是:

    ...select(["count(categories.name) as count","o.*"]) 
             // count(categories.name) here to get total of categories
    .where('categories.name in (:...categories)',{categories})
            // filter by user ids we need
    .groupBy('product.id')
    .having("count = :count", {count:categories.length})
           // here we check if the products has the categories we want 
    .getRawMany();

ps:正如您在评论中提到的那样,这个问题将至少用想要的类别

进行全局示例,其中mysql查询 使用W3School数据库

SELECT o.*,count(o.OrderID) as total FROM OrderDetails as o join Products as p  on o.ProductId = p.ProductID where o.ProductID in (11) group by o.OrderID having total = 3;

As we discuss. this is an example of the query will be:

    ...select(["count(categories.name) as count","o.*"]) 
             // count(categories.name) here to get total of categories
    .where('categories.name in (:...categories)',{categories})
            // filter by user ids we need
    .groupBy('product.id')
    .having("count = :count", {count:categories.length})
           // here we check if the products has the categories we want 
    .getRawMany();

PS: as you mentioned in the comment this question will return the rows with at least the categories wanted

FOR GLOBAL EXAMPLE WITH MYSQL QUERY using W3SCHOOL DATABASE Editor

SELECT o.*,count(o.OrderID) as total FROM OrderDetails as o join Products as p  on o.ProductId = p.ProductID where o.ProductID in (11) group by o.OrderID having total = 3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文