1
例如MySQL的多重選擇我有幾個表:
產品:在一個查詢
| product_id | name | price |
| 1 | apple | 20.32 |
| 2 | pear | 9.99 |
| 3 | banana | 1.5 |
產品屬性:
| attr_id | name | value |
| 1 | weight | 10 kg |
| 2 | date | 2013 |
| 3 | color | red |
...等等。
最後產品屬性關係表:
| product_id | attr_id |
| 1 | 3 |
| 2 | 1 |
| 1 | 2 |
| 3 | 2 |
我的問題:是否有可用的構建體,它返回一個選擇請求查詢產品1和2在下面的數據結構(或類似)?現在我應該首先運行deveral select請求「where product_id IN(1,2)」,然後循環選擇它們的屬性。
對不起,我英文不好:]
array(
[0] = array(
product_id = 1,
name = apple,
attributes= array(
[0] => array(
attr_id = 3,
name = color,
value = red,
),
[0] => array(
attr_id = 2,
name = date,
value = 2013,
)
),
),
[1] = array(
product_id = 2,
name = apple,
attributes= array(
[0] => array(
attr_id = 1,
name = veight,
value = 10 kg,
),
),
)
)
你想獲得產品的嵌套數組,它的屬性數據,就像你的例子?我只是不確定我是否理解你。 – MihanEntalpo
是的,我需要在一個查詢中獲取具有其屬性的多個產品。如果它可行:) – Mindaugas
更好的設計將是如果您保存產品表中的所有屬性... – Justin