3

嗨,我想做一个好的查询以获得一个好的数组。现在例如我有这个查询:

SELECT DISTINCT * FROM products 
        LEFT OUTER JOIN  product_aliases 
        ON product_aliases.product_id = products.id 
        AND product_aliases.alias = '$alias'
        LEFT OUTER JOIN  (product_images
            LEFT OUTER JOIN  product_image_votes 
                    ON product_image_votes.product_image_id = product_images.id)
        ON product_images.product_id = products.id 
WHERE products.id = $id

结果是两个这样的数组:

array(
(int) 0 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
    ),
    'product_image_votes' => array(
        'id' => '2',
        'product_image_id' => '1',
        'vote' => '1',
    )
),
(int) 1 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
    ),
    'product_image_votes' => array(
        'id' => '2',
        'product_image_id' => '1',
        'vote' => '1',
    )
)

问题是:我只想要一个包含产品的唯一数组,例如包含在数组 product_images_votes 中的 product_images。第一个问题是:

  • 有一个唯一的数组而不是两个数组
  • 在我的左连接 annidate 的基础上创建数组内的数组

数组示例:

array(
(int) 0 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
        array('product_image_votes' => array(
            'id' => '2',
            'product_image_id' => '1',
            'vote' => '1',
        ))
    )

有可能吗?我正在使用 php

4

1 回答 1

2

您的查询本身很好,但您需要在 PHP 中构建嵌套。您不能真正单独在 SQL 中生成嵌套结构,因此您必须使用现有的扁平结构。

这可以通过一些创造性的循环来完成。创建一个由 索引的输出数组products['id']。在每次迭代中,如果新条目尚不存在,则创建一个新条目。如果它确实存在,添加到它的product_images,一个也由 索引的数组product_images['id']

// To hold the final array
$output = array();
foreach ($original_array as $row) {

  // Storing these id values to make them easier to refer to...
  $id = $row['products']['id'];
  $pid = $row['product_images']['id'];
  $paid = $row['product_aliases']['id'];
  $pivid = $row['product_image_votes']['id'];

  // Create the product entry if it does not exist
  // and initialize arrays for product_images and product_aliases    
  if (!isset($output[$id])) {
    $output[$id] = array(
      'products' => $row['products'],
      // Initialize these to sub-arrays containing the first ones from this row
      // using the id as the array key
      'product_aliases' => array($paid => $row['product_aliases']),
      'product_images' => array($pid => $row['product_images'])
    );
    // Then add the first vote
    $output[$id]['product_images'][$pid]['product_image_votes'] = array();
    $output[$id]['product_images'][$pid]['product_image_votes'][$pivid] = $row['product_image_votes'];
  }
  // If it does exist already, append the alias if that does not exist, the image, the vote etc.
  else {
    // First add the alias if not already set
    if (!isset($output[$id]['product_aliases'][$paid])) {
      $output[$id]['product_aliases'][$paid] = $row['product_aliases'];
    }
    // Then add the image if not already set
    if (!isset($output[$id]['product_images'][$pid])) {
      $output[$id]['product_images'][$pid] = $row['product_images'];
    }
    // And nest in the image_votes
    if (!isset($output[$id]['product_images'][$pid]['product_image_votes'][$pivid])) {
      $output[$id]['product_images'][$pid]['product_image_votes'][$pivid] = $row['product_image_votes'];
    }
  }
}

这里有很多,很可能我有语法错误或在]某处丢失。祝你好运。

于 2013-01-27T20:29:12.873 回答