Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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 显示某些记录上可能存在联接的表_Mysql_Codeigniter - Fatal编程技术网

Mysql 显示某些记录上可能存在联接的表

Mysql 显示某些记录上可能存在联接的表,mysql,codeigniter,Mysql,Codeigniter,我有两张桌子 表一是文件清单 文档: +-------------+---------------+ | document_ID | document_name | +-------------+---------------+ | 1 | test.pdf | +-------------+---------------+ | 2 | other.pdf | +-------------+---------------+ +-----

我有两张桌子

表一是文件清单

文档:

+-------------+---------------+
| document_ID | document_name |
+-------------+---------------+
|    1        |   test.pdf    |
+-------------+---------------+
|    2        |  other.pdf    |
+-------------+---------------+
+-------------+---------------+-----------+---------------+
| document_ID | user_ID       | has_read  | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    1        |   7           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    2        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
表2是使用过该文档的用户列表。这是一种1-1关系,即用户只会使用一次文档(或者根本不使用)

文档\u用户:

+-------------+---------------+
| document_ID | document_name |
+-------------+---------------+
|    1        |   test.pdf    |
+-------------+---------------+
|    2        |  other.pdf    |
+-------------+---------------+
+-------------+---------------+-----------+---------------+
| document_ID | user_ID       | has_read  | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    1        |   7           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    2        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
当用户浏览列表时,我需要能够显示所有文档,以及他们是否可以访问该列表

因此,例如,上述示例中用户5的期望输出为:

+-------------+---------------+-----------+---------------+
| document_ID | document_name |  has_read | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   test.pdf    |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    2        |  other.pdf    |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
而用户7所需的输出是

+-------------+---------------+-----------+---------------+
| document_ID | document_name |  has_read | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   test.pdf    |     1     |   stuff       |
+-------------+---------------+-----------+---------------+
|    2        |  other.pdf    |           |               |
+-------------+---------------+-----------+---------------+
我试着沿着

$this->db->join('documents_users', 'documents_users.document_ID = documents.document_ID');
$this->db->where('documents_users.user_ID', '7');
return $this->get_all();
但这只返回它们拥有的记录_read=1

我可以看出问题在于“where”命令-但我不确定该怎么办?

您需要采取以下措施:

您需要一个(左)外部联接,将
user\u ID
上的筛选条件移动到联接条件中,而不是位于
WHERE
子句中:

$this->db->join(
   'documents_users',
   'documents_users.document_ID = documents.document_ID
AND documents_users.user_ID = 7',
   'left'
);

eggyal——我怎么能运行同样的查询——并且只返回特定用户没有读过的文档?我试着把“左”改为“内”——这让我知道他们读过的文档。所以我需要在这两个查询之间做一个“区别”?@TheShiftExchange:
$this->db->where('documents\u users.has\u read IS NULL')