我只需要一些方向就可以了。我有以下表格:MySQL動態合併/選擇選擇行
Table: entity
- ID (INT)
Table: attributes
- ID (INT)
- name (VARCHAR)
- internalname (VARCHAR)
Table: values
- ID (INT)
- entity (ID)
- attributes (INT)
- value (Text)
我要的是做一個選擇statment將返回類似以下內容:
- ID = entity.ID
- {attributes.internalname ID 1} = values.{attribe internalname 1}.value
- {attributes.internalname ID 2} = values.{attribe internalname 2}.value
- {attributes.internalname ID 3} = values.{attribe internalname 3}.value
- {attributes.internalname ID n} = values.{attribe internalname n}.value
- etc...
這就好比結合:
SELECT entity.id FROM entity;
和
SELECT (SELECT values.value FROM values WHERE values.entity = entity.ID AND values.attributes = attributes.ID) FROM attributes;
這是一件很難解釋的事情,但是如果你需要我進一步解釋,請告訴我。
我實際上想要將屬性中的所有值都轉換爲列,並將所有值轉換爲其對應屬性的值,並將ID作爲選擇器。
我給查詢一個ID(元素ID),並在一個結果行中返回所有的數據。
在此先感謝!
我不明白爲什麼表實體存在只有一列,它已存儲在值表 – cfEngineers 2011-01-11 19:29:17
它只是作爲一個索引,並且可能會出現幾列。這實際上與Magento Commerce存儲數據的方式非常相似,問題是他們使用數百個查詢來獲得我試圖在一箇中實現的相同結果。 – Nitroware 2011-01-11 20:52:46