mysql - 我如何在 CodeIgniter 中查询这个

标签 mysql database codeigniter

这就是我想要的:

SELECT DISTINCT first_name,last_name 
FROM employees e 
INNER JOIN salaries s ON e.emp_no = s.emp_no 
WHERE e.birth_date > '1963-01-01' 
AND s.salary>150000

我已经尝试过了,除了得到了我想要的名字之外,还得到了另外 12 个名字。有问题。

    $this->db->distinct();
    $this->db->select('first_name, last_name');
    $this->db->from('employees');
    $this->db->join('salaries s', 'e.emp_no = s.emp_no', 'inner');
    $this->db->where('e.birth_date > 1963-01-01');
    $this->db->where('s.salary > 150000');

    $result = $this->db->get();

最佳答案

我添加了一条评论,但我会将其添加为建议答案。您没有在 $this->db->from('employees'); 中为 employees 添加别名。 在员工后面添加 e,就像在工资 s 中所做的那样。

$this->db->distinct();
$this->db->select('first_name, last_name');
$this->db->from('employees e');
$this->db->join('salaries s', 'e.emp_no = s.emp_no', 'inner');
$this->db->where('e.birth_date > 1963-01-01');
$this->db->where('s.salary > 150000');

$result = $this->db->get();

编辑: where 子句中的日期字符串不加引号。尝试更新语句以引用字符串,或将其作为第二个参数传递。

$this->db->where('e.birth_date > "1963-01-01"');

$this->db->where('e.birth_date >', '1963-01-01');

我假设这是数据库中日期列的正确格式掩码。

关于mysql - 我如何在 CodeIgniter 中查询这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220097/

相关文章:

mysql - 多表 DELETE 语句是不好的做法吗?为什么?

mysql - 需要了解 iPhone 上基于 Web 的客户端服务器应用程序

php - 如何在 CodeIgniter 的 url 中隐藏 Controller 名称?

codeigniter - 在 CodeIgniter 中将数据导出为 CSV

php - 在表单中用作 GET 的多个 OPTION 值

将行值转换为列的 MySql 准备语句

mysql - 如何保持数据库中某些实体的更新(差异)

java - 在 Java 生产中使用的最佳 OO-DBMS 是什么?

MySQL 创建 View ,该 View 是两个表的并集,其中一个表值带有前缀

具有良好用户登录/管理的 PHP 框架