【发布时间】: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
但我知道我有不止一种材料。
【问题讨论】:
-
请阅读此meta.stackoverflow.com/questions/333952/… 并编辑您的帖子
标签: mysql sql database drupal-8