【问题标题】:SQL join into join create a good arraySQL join into join 创建好数组
【发布时间】:2013-01-11 03:30:33
【问题描述】:

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

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

【问题讨论】:

    标签: php sql arrays join left-join


    【解决方案1】:

    您的查询是好的,但是您需要在 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'];
        }
      }
    }
    

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

    【讨论】:

    • 语法错误没问题,认为在我的查询中我有很多连接,我已经计算了要连接到我的查询中的表:40 个要解析的表.. OMG.. 我认为有是一种方法或什么来帮助我而不是解析所有结果
    • @AlessandroMinoccheri 好吧,这就是 ORM 或像 CakePHP 这样的框架有用的地方。表关系在对象模型中定义,然后您检索一个产品,然后调用方法来获取其关联的图像等。如果没有框架,您必须手动完成大量工作。
    • 这个查询是在 cakephp 中完成的,但我有太多的包含并进行 5000 个查询。在这个问题stackoverflow.com/questions/14487038/… 之后,我决定手动编写查询
    • @AlessandroMinoccheri 那就是权衡。该框架会生成大量查询并且速度很慢,或者您构建一个快速查询并且必须自己解析结果。
    • 是一个艰难的选择,但我认为手动构建查询更有效。但是要加入 40 个表,我将花费大量时间来解析它。但如果这是我必须这样做的唯一方法
    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多