0

我有以下查询:

$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

2

为了更容易阅读您的代码(因此您不需要在任何地方都有这么多的连接和引号,您可以执行如下查询:

$sql='
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
        left join '.TABLE_ORDERS_PRODUCTS_ATTRIBUTES.' Attrib
            on op.orders_products_id=Attrib.orders_products_id
where 
    orders_id = "' .(int)$cuttinglist['orders_id'] . '"
';
$products_query = tep_db_query($sql);
于 2012-09-05T11:31:19.243 回答
1
$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) "
              " left join " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " pa " .
              " on (op.products_id = pa.orders_products_id) where orders_id = '" . 
              (int)$cuttinglist['orders_id'] . "'");
于 2012-09-05T11:31:09.167 回答