为了避免多个 mysql SELECT 查询,我尝试将 SELECT 从 foreach 循环中取出。
查询是
SELECT o.orders_id,
o.date_purchased,
o.orders_status,
ot.text AS order_total,
os.orders_status_name
FROM " . TABLE_ORDERS . " o
JOIN " . TABLE_ORDERS_TOTAL . " ot ON (o.orders_id = ot.orders_id AND ot.class = 'ot_total')
JOIN " . TABLE_ORDERS_STATUS . " os ON (o.orders_status = os.orders_status_id AND os.language_id = " . (int)$_SESSION['languages_id'] . ")
WHERE o.customers_id = " . (int)$_SESSION['customer_id'] . "
ORDER BY orders_id DESC
结果我得到了一个名为 $history_result['RESULT'] 的数组,它看起来像
Array
(
[0] => Array
(
[orders_id] => 309
[date_purchased] => 2013-10-02 15:49:54
[orders_status] => 1
[order_total] => 9,00 €
[orders_status_name] => Offen
)
[1] => Array
(
[orders_id] => 308
[date_purchased] => 2013-10-02 15:39:54
[orders_status] => 1
[order_total] => 9,00 €
[orders_status_name] => Offen
)
[2] => Array
(
[orders_id] => 307
[date_purchased] => 2013-10-02 15:33:48
[orders_status] => 1
[order_total] => 9,00 €
[orders_status_name] => Offen
)
)
在下面的 foreach-loop 中是下一个 MySql-Query
foreach ($history_result['RESULT'] as $history) {
$trackings = $db->result("-- 03 account_history_info.php
SELECT o.ortra_parcel_id,
c.carrier_tracking_link,
c.carrier_name
FROM " . TABLE_ORDERS_TRACKING . " o
JOIN " . TABLE_CARRIERS . " c ON o.ortra_id = c.carrier_id
WHERE ortra_order_id = " . $history['orders_id']
);
}
如何直接访问数组元素“orders_id”,以便创建类似的 SQL 语句
$trackings = $db->result("-- 03 account_history_info.php
SELECT o.ortra_parcel_id,
c.carrier_tracking_link,
c.carrier_name
FROM " . TABLE_ORDERS_TRACKING . " o
JOIN " . TABLE_CARRIERS . " c ON o.ortra_id = c.carrier_id
WHERE ortra_order_id IN = " . implode(',', $fooArray)
);