这就是我想要的:
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'); 中为
在员工后面添加 e,就像在工资 s 中所做的那样。employees
添加别名。
$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/