php - 如何获取表2中没有的值

标签 php sql codeigniter

如何获取table_1中不在table_2中的id
我的 table 看起来像这种类型。
表_1

---------------------------------------------
|  id      |    user                        |
---------------------------------------------
|   1      |    Jack                        |
---------------------------------------------
|   2      |    John                        |
---------------------------------------------

表_2

------------------------------------------
|  web_id      |    website              |
------------------------------------------
|   2          |   Facebook              |
------------------------------------------
|   3          |   Google+               |
------------------------------------------

我想要codeigniter查询

$this->db->select("*");
        $this->db->from("table_1");
        $this->db->where_not_in('id', "table_2.web_id");
        return $this->db->get();

最佳答案

试试这个

 SELECT id
 FROM table_1
 WHERE id NOT IN
 (
       SELECT web_id FROM table_2 WHERE 1
 );

使用 CodeIgniter Active Records 查询如下

  $this->db->select('*');
  $this->db->where('id NOT IN (SELECT web_id FROM table_2 WHERE 1)', NULL, FALSE);
  $query = $this->db->get('table_1');

关于php - 如何获取表2中没有的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221155/

相关文章:

php - 循环遍历日期以查找特定日期 php SQL

选择用户的非好友的 MySQL 查询

c# - 用 SQL 列填充 WPF 组合框

php - 使用 DMZ Data Mapper 连接到多个数据库

php - 在 codeigniter 中注销

java - PHP 客户端和 Java 服务器之间的客户端-服务器网络

php - 适当的缓存——应该如何保存查询结果的数量?

php - php 中 ftp 的替代品

mysql - 获取两个日期之间的日期列表

php - codeigniter 3 get_where sql查询函数