php - 如何使用查询 CI 语法编写 SQL 查询?

标签 php mysql codeigniter

我有以下查询,我需要在 Codeignitor 的模型中编写

SELECT DISTINCT make FROM equipment_nonconnected_master WHERE Equipment_NonConnected_Type_Master_ID IN (select Equipment_NonConnected_Type_Master_ID FROM equipment_nonconnected_type_master)

我只知道简单地用 Codeignitor 编写的选择查询,如下所示:

示例:

$this->db->distinct();
$this->db->select('make');
$this->db->order_by('make', 'asc');
$query = $this->db->get('carriermodels');

谁能帮我用 CI 语法写查询

最佳答案

您可以使用简单的where() 函数来做到这一点

$subquery="SELECT 
    Equipment_NonConnected_Type_Master_ID 
  FROM
    equipment_nonconnected_type_master";
$this->db->distinct();
$this->db->select('make');
$this->db->from('carriermodels');
$this->db->where('Equipment_NonConnected_Type_Master_ID IN('.$subquery.')');
$this->db->order_by('make', 'asc');
$query = $this->db->get();

或者更好地使用 join

$this->db->distinct();
$this->db->select('c.make');
$this->db->from('carriermodels c');
$this->db->join('equipment_nonconnected_type_master m','c.Equipment_NonConnected_Type_Master_ID =m.Equipment_NonConnected_Type_Master_ID ');
$this->db->order_by('c.make', 'asc');
$query = $this->db->get();

关于php - 如何使用查询 CI 语法编写 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908748/

相关文章:

php - 如何在全文 MySQL 搜索中同时匹配 htmlentified 关键字和非 htmlentified 关键字

python MySQL : clean multiple foreign keys table

php - 将我的两次 mysql 调用减少为一次

codeigniter - base_url() 在代码点火器 1.7.3 中出现错误

php - Yii2 excel导入 'PHPExcel_IOFactory'未找到

php - 传递表单变量的安全方式

mysql - Mysql 未初始化常量

php - MySQL/PHP如何从与同一行的另一个字段相关的字段中获取特定字段id

php - 使用Codeigniter框架连接DSN

php - 在 symfony 中禁用路由缓存