我的任务是获取包含关联品牌的所有产品的列表。Mysql - 从连接表和两个查询中将值导入列
我已经准备好以下表格:
posts | term_relationships | term_taxonomy | terms
---------|------------------------|------------------|---------
ID | object_id | term_taxonomy_id | term_id
post_type| term_taxonomy_id | term_id | name
brand_id | | taxonomies
它们以下列方式有关:
posts.ID -> term_relationships.object_id
term_relationships.term_taxonomy_id -> term_taxonomy.term_taxonomy_id
term_taxonomy.term_id -> terms.term_id
领域之间的关联是由分配ID为每个连接映像的脚本制作:
在这一点上,如果关联存在,我必须开发查询来分配posts.brand_id值terms.term_id。
筛选标准是:
term_taxonomy.taxonomies="product_brand"
posts.post_type= "products"
查询逻辑可能是:
IF
term_taxonomy.taxonomies="product_brand" AND
term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id AND
term_relationships.object_id = posts.ID
THEN
INSERT INTO terms_term_id = brand_id
ELSE
brands.brand_id IS NULL
我试图把它翻译成MySQL,但没有成功。你有这个建议吗?
所以我要尝试一下意见。罗兰对不起,OT代表什么? – Alex
OT = Off-Topic ...在上面的查询中,您有两个同名的列。您可能必须至少使用其中一个别名:'mg_term_relationships.term_taxonomy_id AS term_relation_taxonomy_id'或者可以重命名该列(如果需要)。 – Roland
是的。我修好了,现在可以使用了。谢谢罗兰! – Alex