1

我有以下查询:

$cuttinglist_products_query = 
  tep_db_query("select op.orders_products_id, op.orders_id, op.products_id, ".
                      "op.products_model, op.products_name, op.products_quantity, ".
                      "p.products_id from " . TABLE_ORDERS_PRODUCTS . " op " .
                  " left join " . TABLE_PRODUCTS . " p " .
                  " on (op.products_id = p.products_id) where orders_id = '" . 
                  (int)$cuttinglist['orders_id'] . "'");

这将两个具有相同 ID 的表连接在一起。我将如何加入第三张桌子,名为:

"TABLE_ORDERS_PRODUCTS_ATTRIBUTES" 

具有与以下相同的 ID:

"TABLE_ORDERS_PRODUCTS"

使用的ID是:

"orders_products_id"
4

2 回答 2

5

只需添加另一个连接子句:

SELECT ...
FROM TABLE_ORDERS_PRODUCTS op
LEFT JOIN TABLE_PRODUCTS p ON op.products_id = p.products_id
LEFT JOIN TABLE_ORDERS_PRODUCTS_ATTRIBUTES pa ON op.products_id = pa.orders_products_id
WHERE ...
于 2012-09-03T16:26:49.130 回答
0
$sql  = " select ";
$sql .= "   op.orders_products_id,  op.orders_id,          op.products_id,  op.products_model, ";
$sql .= "   op.products_name,       op.products_quantity,  p.products_id ";
$sql .= " from ". TABLE_ORDERS_PRODUCTS ." op ";   
$sql .= " left join ". TABLE_PRODUCTS ." p on (op.products_id = p.products_id) ";
$sql .= " left join ". TABLE_ORDERS_PRODUCTS_ATTRIBUTES ." pa on (pa.orders_products_id = op.orders_products_id) ";
$sql .= " where op.orders_id = ";
$sql .= "'". (int)$cuttinglist['orders_id'] ."'";

$cuttinglist_products_query = tep_db_query( $sql );
于 2012-09-03T16:44:41.143 回答