【问题标题】:My left join union right join isn't quite working - I'm trying to simulate a full outer join我的左连接联合右连接不太工作 - 我正在尝试模拟一个完整的外部连接
【发布时间】:2019-12-15 14:35:14
【问题描述】:

我正在尝试对 Drupal 8 中的几个表进行完全外连接。MySQL 不支持完全外连接,所以我使用左连接联合右连接。

它获取大部分数据,但在我查询的材料中,我知道有多种材料,但我只得到一种。我看不出我做错了什么。

我直接访问数据库而不使用辅助函数,因为这实际上是针对我正在编写的移动应用程序,它从手机或平板电脑查询数据库:

    $query_phrase = "select 
                     commerce_product_field_data.title as title,
                     commerce_product_field_data.product_id as product_id,
                     commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
                     commerce_product__field_barcode.field_barcode_value as barcode,
                     commerce_product__field_price.field_price_number as price,
                     commerce_product__field_price.field_price_currency_code as currency_code,
                     commerce_product__field_sku.field_sku_value as sku,
                     node_field_data.title as material_name,
                     file_managed.uri as uri,
                     file_managed.filename as filename
                     
                     from {commerce_product_field_data}
                     
                     left join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
                     left join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
                     left join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
                     left join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
/*not working*/      left join commerce_product__field_materials  on product_id = commerce_product__field_materials.entity_id
/*not working*/      left join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
                     left join file_managed on product_id = file_managed.fid";

       $query_phrase .= " union ";
                     
    $query_phrase .= "select 
                     commerce_product_field_data.title as title,
                     commerce_product_field_data.product_id as product_id,
                     commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
                     commerce_product__field_barcode.field_barcode_value as barcode,
                     commerce_product__field_price.field_price_number as price,
                     commerce_product__field_price.field_price_currency_code as currency_code,
                     commerce_product__field_sku.field_sku_value as sku,
                     node_field_data.title as material_name,
                     file_managed.uri as uri,
                     file_managed.filename as filename
                     
                     from {commerce_product_field_data}
                     
                     right join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
                     right join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
                     right join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
                     right join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
/*not working*/      right join commerce_product__field_materials  on product_id = commerce_product__field_materials.entity_id
/*not working*/      right join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
                     right join file_managed on product_id = file_managed.fid";
                     
       $query_phrase .=  " where commerce_product__field_barcode.field_barcode_value = :barcode";
                     
                     
    $query = db_query($query_phrase, array(":barcode" => "334242"));

当我调试我的结果时,我得到:

Array
(
    [title] => Dummy Product
    [product_id] => 2
    [has_multiple_configuration] => 0
    [barcode] => 334242
    [price] => 325.000000
    [currency_code] => USD
    [sku] => dummy sku
    [material_name] => test material
    [uri] => public:a

但我知道我有不止一种材料。

【问题讨论】:

标签: mysql sql database drupal-8


【解决方案1】:

您在查询的left join 部分中缺少where 子句。

$query_phrase = "select 
     commerce_product_field_data.title as title,
     commerce_product_field_data.product_id as product_id,
     commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
     commerce_product__field_barcode.field_barcode_value as barcode,
     commerce_product__field_price.field_price_number as price,
     commerce_product__field_price.field_price_currency_code as currency_code,
     commerce_product__field_sku.field_sku_value as sku,
     node_field_data.title as material_name,
     file_managed.uri as uri,
     file_managed.filename as filename

     from {commerce_product_field_data}

     left join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
     left join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
     left join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
     left join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
     left join commerce_product__field_materials  on product_id = commerce_product__field_materials.entity_id
     left join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
     left join file_managed on product_id = file_managed.fid";

   $query_phrase .=  " where commerce_product__field_barcode.field_barcode_value = :barcode";

   $query_phrase .= " union ";

$query_phrase .= "select 
     commerce_product_field_data.title as title,
     commerce_product_field_data.product_id as product_id,
     commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
     commerce_product__field_barcode.field_barcode_value as barcode,
     commerce_product__field_price.field_price_number as price,
     commerce_product__field_price.field_price_currency_code as currency_code,
     commerce_product__field_sku.field_sku_value as sku,
     node_field_data.title as material_name,
     file_managed.uri as uri,
     file_managed.filename as filename

     from {commerce_product_field_data}

     right join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
     right join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
     right join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
     right join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
     right join commerce_product__field_materials  on product_id = commerce_product__field_materials.entity_id
     right join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
     right join file_managed on product_id = file_managed.fid";

   $query_phrase .=  " where commerce_product__field_barcode.field_barcode_value = :barcode";

【讨论】:

  • 这似乎并没有解决它,但它确实给了我更多关于 sql 的信息......我只是想尽可能多地运行 2 个查询......不过谢谢你
  • 我怀疑还有其他问题。您的错误会导致您获得太多行,而不是太少。所以问题出在联合中的单个查询中。
  • 您通常不应该在WHERE 子句中对左连接表设置条件,因为这会过滤掉该表中所有不匹配的行。条件应该在与该表连接的ON 子句中。
  • 当你有这么多表时,我不确定模拟完全外连接的方法有多好。您可能需要更多组合。
猜你喜欢
  • 2014-02-24
  • 2015-01-11
  • 2016-12-13
  • 2014-09-23
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
相关资源
最近更新 更多