我需要你對這個自定義查詢的幫助,對我來說有點太難了。下面是MY_TABLE一個例子:MySQL查詢選擇與IF(xxxx已存在)遞歸
_______________________________
| item_name | item_slug |
-------------------------------
| Black Pant | pant_black | Select : pick first color, 'pant' doesn't exists
| Red Pant | pant_red | Variation: 'pant_black'
| Yellow Pant | pant_yellow | Variation: 'pant_black'
| Tshirt | tshirt | Select : 'tshirt' exists
| Tshirt Black | tshirt_black | Variation: 'tshirt'
| Tshirt Red | tshirt_red | Variation: 'tshirt'
| Tshirt Yellow| tshirt_yellow| Variation: 'tshirt'
-------------------------------
我試圖得到的結果是:
如果( 'T恤' & & 'tshirt_black' 存在):
不顯示 'tshirt_black' , 'tshirt_red', 'tshirt_yellow' 顏色,並把它們在一個 '變化' 陣列:
tshirt
| Variations:
|-> tshirt_black
|-> tshirt_red
|-> tshirt_yellow
如果( '褲' 不存在& & 'pant_black' 存在):
以發現 'pant_black' 第一顏色,不顯示 'pant_red', 'pant_yellow' 顏色,並把它們在一個 '變化' 陣列:
pant_black
| Variations:
|-> pant_red
|-> pant_yellow
注意:'褲子'不存在,只有顏色存在。
我設法讓時無色名存在與此查詢(「T恤」),它的工作:
SELECT * FROM my_table WHERE item_name NOT LIKE '% Black' AND item_name NOT LIKE '% Red' AND item_name NOT LIKE '% Yellow'
然後我申請一個PHP的foreach內的第二查詢()把「T恤」的變化在陣列。
SELECT * FROM my_table WHERE item_slug LIKE '$item_slug%' AND item_slug != '$item_slug'
但我不能找到一個方法來遞歸檢查(「喘氣」 donesn't存在),然後使用發現爲「基地」的第一種顏色。例如'pant_black'。
完美的解決方案將是使其成爲一體化查詢。你怎麼看?
編輯:編輯我的帖子,只使用'item_slug'。
我沒有看到你想要的結果與你說的查詢有什麼關係。你能否更好地解釋你想要的結果是什麼? –
如果我不夠清楚,我很抱歉,我編輯了這篇文章,希望你現在能夠理解:) – hawkidoki