项目中需要使用到union all联合查询,查看了yii2的中文手册和百度了一下,没有说明有union all 相关的查询方法。只有简单的介绍怎么用union。又因为union会去重。
无奈查看源码,发现query.php(路径yii2\db\Query.php)中有关于union方法有两行注释:
/**
* Appends a SQL statement using UNION operator.
* @param string|Query $sql the SQL statement to be appended using UNION
* @param boolean $all TRUE if using UNION ALL and FALSE if using UNION
* @return $this the query object itself
*/
public function union($sql, $all = false)
{
$this->union[] = ['query' => $sql, 'all' => $all];
return $this;
}
原来 union(),默认第二参数为false,所以在构造union()方法的时候传入第二个参数,会使用union All 查询。
问题解决。是因为union()查询用的比较少吗,还是yii2.0在国内用的人不多,百度半天都没有相关的问题。…