让我们想象一下这样的事情:
SELECT * FROM products LEFT JOIN prices ON prices.PRODUCT_ID = products.ID;
这给出了:
ID: 1; NAME: 'product with no price'; PRODUCT_ID: null; PRICE: null;
ID: 2; NAME: 'product with one price'; PRODUCT_ID: 2; PRICE: 1000;
ID: 3; NAME: 'product with two prices'; PRODUCT_ID: 3; PRICE: 2000;
ID: 3; NAME: 'product with two prices'; PRODUCT_ID: 3; PRICE: 2500;
这个算法应该产生:
array:
product => (
[0]
id = 1
name = product with no price
prices = array()
[1]
id = 2
name = product with one price
prices = array(
product_id: 2,
price: 1000
)
[2]
id = 3
name = product with two prices
prices = array(
array(product_id: 3,
price: 2000),
array(product_id: 3,
price: 2500)
)
并且不要忘记传递连接。所以价格 -> 产品,但可以是 commentOnPrice -> 价格 -> 产品。是否有可能创建这种复杂的算法?