php - SQL 条件 UNION() 与 Yii 查询生成器

标签 php mysql yii

我想准备一个 SQL 查询,我想在其中放入 UNION 查询,但是 UNION 查询是基于特定条件的,是否可以使用 Yii 查询构建器进行。

下面是我用简单的 DAO 制作的示例代码,但我想以查询生成器风格制作它

    if(isset($first) && $first!="")
       $con .= "SELECT id,first, from user where first LIKE '%".$first."%'";
   if(isset($last) && $last!="")
       $con .= " AND last LIKE '%".$last."%'";
   if((!empty($first) || !empty($last)) && (!empty($flag)) )
       $con .= " UNION SELECT id,first, from user where flag = 1"

   $command    = $connection->createCommand($con)->queryall();

最佳答案

是的,确实如此。假设这是您的测试代码:

$command = Yii::app()->db->createCommand();

if(<your if statements here) {
    $command->select('id, username, profile')
}

if(<union if statement>) {
    $command->union()
}

一些来自 Query Builder page 的其他行样本

->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();

但是您要查看的主要内容是 union()

关于php - SQL 条件 UNION() 与 Yii 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474345/

相关文章:

php - 使用 preg_replace 缩小 CSS

php - 从 Postcode DB 获取 lat/lng 并在 PHP/MYSQL 中按最近的顺序排序

php - 是否可以在 Laravel 中引用不同数据库中的外键?

php - Apache 的 x-sendfile 替代方案,用于下载带有恢复支持的大文件

php - Yii 中 BELONGS_TO 的关系事件记录

php - Symfony2 中的设计模式 : is EventDispatcher a Mediator or Event Aggregator?

javascript - 将数据从 PHP 传递到 angular.js

java - Hibernate 给我无效的更新 sql 查询

php - 无法连接到 'IP' 上的 MySQL 服务器 (4)

php - 使用 PHP 将 XML 转换为 HTML 表格