Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在CodeIgniter中查询此问题_Mysql_Database_Codeigniter - Fatal编程技术网

Mysql 如何在CodeIgniter中查询此问题

Mysql 如何在CodeIgniter中查询此问题,mysql,database,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(); $th

这就是我想要的:

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"');


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

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

$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);           

在where子句中,应在第一个参数中输入运算符,以与第二个参数(值)进行比较

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

您在“$this->db->from('employees');”中没有别名
employees
在员工之后添加
e
,就像您在
薪水s
中所做的那样。我最初在员工之后添加了e,我尝试了不同的方法,但在粘贴到此处时忘记了将其包括在内。在phpmyadmin中运行sql查询时,这给了我15个名称,只返回3。@ethio我看到语句中没有引用日期字符串。您应该引用字符串,或者将其作为第二个参数传入。我已经更新了答案。是的,我就是这么做的,并且回答了这个问题,谢谢。@ethio太好了。如果提供了正确的答案,我们鼓励您标记。我在编辑前45分钟留下了一个答案,我将把它标记为我的答案。
$this->db->where('e.birth_date >', 1963-01-01);
$this->db->where('s.salary >', 150000');