我的任務是獲取包含關聯品牌的所有產品的列表。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