【发布时间】:2021-09-10 02:55:15
【问题描述】:
我创建了 3 个表
Products
|product_id | product_name|
|-----------|-------------|
|1 | coca cola |
|2 | pepsi |
|3 | orange |
Product_tags
|product_id | tag_id |
|-----------|--------|
|1 | 1 |
|2 | 1 |
|3 | 2 |
Tag_names
|tag_id | tag_name |
|-----------|----------|
|1 | soda |
|2 | juice |
使用:
db.products.belongsToMany(db.tag_names, { through: 'product_tags', as: "tags", foreignKey: 'product_id'});
db.tag_names.belongsToMany(db.products, {through: 'product_tags', as: "products", foreignKey: 'tag_id'});
现在我想通过产品名称或标签名称搜索这些表。例如产品具有名称“coca cola”和标签“soda”,因此搜索“coca”或搜索“sod”都会返回它,“sod”也会返回“pepsi”。我似乎所能做的就是让它同时要求 product_name 和 tag_name 来匹配不是我想要的查询。
谢谢,
丹尼
【问题讨论】:
标签: mysql node.js sequelize.js