php - 如何在我在查询中有查询的情况下返回结果

标签 php mysql

我在查询中写了一个查询,当 fname(表 1 的字段)不等于名称(表 2 的字段)时,我想返回结果。但是当我编译这个查询时,我得到了 fatal error 。请检查下面的代码行。并告诉我哪里错了。

table1: 等于ult_camp

table2: 等于 ult_coach

$rs = $this->db->select('ult_camp.*,ult_country.name as country_name,ult_state.name as state_name')
                    ->join('ult_country', 'ult_country.id = ult_camp.country_id')
                    ->join('ult_state', 'ult_state.id = ult_camp.state_id ')
                    ->from('ult_camp')
                    ->where("'org_fname' NOT IN (SELECT 'name' FROM 'ult_coach')", NULL, FALSE)
                    ->where('ult_camp.status', 'Active')
                    ->where('ult_state.status', 'Active')
                    ->where('ult_country.status', 'Active')
                    ->get()->result(); 

请帮我解决这个问题。我们将不胜感激。

提前致谢。

最佳答案

您的第一个 WHERE 函数调用使用的是原始 SQL,因此您当前拥有的将无法工作。您可以尝试使用 whereRaw 代替:

->whereRaw("org_fname NOT IN (SELECT name FROM ult_coach)")
->where('ult_camp.status', 'Active')
...

但您也可以将您的查询表述为 ult_coach 表的左连接:

$rs = $this->db->select('ult_camp.*,ult_country.name as country_name,ult_state.name as state_name')
                ->join('ult_country', 'ult_country.id = ult_camp.country_id')
                ->join('ult_state', 'ult_state.id = ult_camp.state_id ')
                ->leftJoin('ult_coach', 'ult_camp.org_fname = ult_coach.name')
                ->from('ult_camp')
                ->whereNull('ult_coach.name')
                ->where('ult_camp.status', 'Active')
                ->where('ult_state.status', 'Active')
                ->where('ult_country.status', 'Active')
                ->get()->result(); 

关于php - 如何在我在查询中有查询的情况下返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46463399/

相关文章:

php - 谷歌搜索中的 seo 元描述问题

php - 我的 GET 变量被转义了吗?

php - 为什么我的 html/php/ajax 返回查询中的所有行

MySQL 如果不存在则从一列插入到另一列

mysql - 数据表 - 从不同的 Laravel 查询创建每一列

php - 如何使用 PHP Composer 定义私有(private)类路径?

php - 创建 Web 应用程序的演示

php - 表单验证以及数据库错误

Mysql触发器插入最后日期

mysql - 反向隧道手动工作,不适用于复制