2013-04-25 34 views
2

查询这这就是我想要的:我怎样才能笨

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(); 
+0

您没有在'$ this-> db->中从'('employees')别名'employees';'在像您这样的员工为'slaries s'工作之后添加'e'。 – Wolf 2013-04-25 16:34:30

回答

0

在where子句中,经营者应在第一个参数被输入到与第二个参数的值进行比较。

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

我添加了一个评论,但我会添加它作为一个建议answer.You没有别名employees$this->db->from('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'); 

我假定这是在你的数据库中的日期栏正确的格式掩码。

+0

我最初在员工之后添加了e,我尝试了不同的东西,并在这里粘贴时忘了包含它。这给了我15个名字,同时在phpmyadmin中运行sql查询只返回3. – ethio 2013-04-26 11:44:54

+0

@ethio我看到日期字符串没有在语句中引用。您应该引用该字符串,或将其作为第二个参数传入。我已经更新了答案。 – Wolf 2013-04-26 15:21:44

+0

是的,这就是我所做的并回答了这个问题,无论如何。 – ethio 2013-04-26 23:34:47

0

如果活动记录让你感到困惑,那很容易。你可以简单的使用查询功能

$query = "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"; 
$this->db->query($query);