3

以下のクエリをサブクエリに変換するにはどうすればよいですか? サブクエリを介して同じことをしたいJOINSを使用したくない。つまり、3 つの結合のうち 3 つのサブクエリが必要です。以下のクエリはどのように可能でしょうか?

protected $_name = 'sale_package_features';
public function getAllSalePackageFeatures(){
    $sql = $this->select()->setIntegrityCheck(false)
            ->from(array('spf' => $this->_name))
            ->joinLeft(array('sd' => 'sale_devices'),'sd.sale_device_id = spf.sale_device_id',array('sd.sale_device_name AS deviceName'))
            ->joinLeft(array('sp' => 'sale_packages'),'sp.sale_package_id = spf.sale_package_id',array('sp.sale_package_name AS packageName'))
            ->joinLeft(array('sf' => 'sale_features'),'sf.sale_feature_id = spf.sale_feature_id',array('sf.sale_feature_name AS featureName'))
            ->where('sf.parent_id != ?',0)
            ->order('spf.sale_package_feature_id ASC');
    return $sql->query()->fetchAll();
}

編集:

SELECT
   `spf`.*, `sd`.`sale_device_name` AS `deviceName`, 
   `sp`.`sale_package_name` AS `packageName`, 
   `sf`.`sale_feature_name` AS `featureName` 
FROM `sale_package_features` AS `spf` 
LEFT JOIN `sale_devices` AS `sd` 
  ON sd.sale_device_id = spf.sale_device_id
LEFT JOIN `sale_packages` AS `sp` 
  ON sp.sale_package_id = spf.sale_package_id 
LEFT JOIN `sale_features` AS `sf` 
  ON sf.sale_feature_id = spf.sale_feature_id 
WHERE (sf.parent_id != 0) 
ORDER BY `spf`.`sale_package_feature_id` ASC
4

1 に答える 1

0

使用できます

$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$subSelect = $dbAdapter
    ->select()
    ->from( 'tablename',
        array(
            'col1_alias' => 'column1_name',
            'col2_alias' => 'column2_name',
        ))
    ->where('somefield = ?', $value_to_be_quoted);

$select = $dbAdapter
    ->select()
    ->from(array('sub_alias' => $subSelect ))
    ->where('otherfield = ?', $other_value_to_be_quoted);

$sql = $select->assemble();

$sql含まれます

SELECT 
    sub_alias . *
FROM
    (SELECT 
        tablename.column1_name AS col1_alias,
            tablename.column2_name AS col2_alias
    FROM
        tablename
    WHERE
        (somefield = 'quoted_value')) AS sub_alias
WHERE
    (otherfield = 'quoted_other_value')

SELECT .. WHERE x IN (subselect)一緒に行くよりも 使用する必要がある場合

$select->where( 'column IN (?)', new Zend_Db_Expr($subselect->assemble()) );
于 2013-07-30T16:05:40.480 に答える