写入LEFT JOIN有错误

时间:2017-11-23 06:25:58

标签: php sql drupal-modules

我有以下原始SQL:

    SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, 'node' AS field_data_body_node_entity_type
    FROM 
    {node} node
    LEFT JOIN {field_data_commerce_node_checkout_node} field_data_commerce_node_checkout_node ON node.nid = field_data_commerce_node_checkout_node.commerce_node_checkout_node_target_id AND (field_data_commerce_node_checkout_node.entity_type = 'commerce_line_item' AND field_data_commerce_node_checkout_node.deleted = '0')
    LEFT JOIN {commerce_line_item} commerce_node_checkout_node_node ON field_data_commerce_node_checkout_node.entity_id = commerce_node_checkout_node_node.line_item_id
    WHERE (( (node.status = '1') AND (node.type IN  ('myproducts')) ))
    LIMIT 5 OFFSET 0

我有一个模块,我需要将这个查询添加到drupal commerce中带有body字段的用户产品列表...

这是我在$ query中添加的内容。

    $query = db_select('{node}', 'node');
    $query->fields('node', array('title', 'nid', 'created'));
    $query->leftJoin('{field_data_commerce_node_checkout_node}', 'field_data_commerce_node_checkout_node', 'node.nid = field_data_commerce_node_checkout_node.commerce_node_checkout_node_target_id AND (field_data_commerce_node_checkout_node.entity_type = 'commerce_line_item' AND field_data_commerce_node_checkout_node.deleted = '0')');
    $query->leftJoin('{commerce_line_item}', 'commerce_node_checkout_node_node', 'field_data_commerce_node_checkout_node.entity_id = commerce_node_checkout_node_node.line_item_id');
    $query->condition('node.status', 1);
    $query->condition('node.type', array(myproducts), 'IN');
    $query->range(0, 5);
    $result = $query->execute();

如何编写没有错误的代码?

1 个答案:

答案 0 :(得分:0)

您遇到了一些语法错误。我解决了它们并以真实的方式重写了您的查询。检查并通知我结果

我建议看看db_select documentation强烈建议写错误以获得更好的答案dadash

$query = db_select('node', 'n');
$query->fields('n', array('title', 'nid', 'created'));
$query->leftJoin('field_data_commerce_node_checkout_node', 'fcn', 'n.nid = fcn.commerce_node_checkout_node_target_id');
$query->leftJoin('commerce_line_item', 'cli', 'fcn.entity_id = cli.line_item_id');
$query->condition('fcn.entity_type', 'commerce_line_item');
$query->condition('fcn.deleted', 0);
$query->condition('n.status', 1);
$query->condition('n.type', array($myproducts), 'IN');
$query->range(0, 5);
$result = $query->execute();