gpt4 book ai didi

php - NULL 上的第三次连接不返回值(Codeigniter)

转载 作者:行者123 更新时间:2023-11-29 03:21:13 25 4
gpt4 key购买 nike

我有一个不返回所需值的连接语句。

原因是第三个表 (cvi) 在许多情况下不会包含与第四个表 (cmi) 匹配的任何行。当没有值连接时,结果不会显示表 ccv 中的所需值。

    /* Tables */
$this->db->from($this->table_c.' c');
$this->db->join($this->table_cv.' cv', 'cv.content_id = c.id', 'inner');
$this->db->join($this->table_cvi.' cvi', 'cvi.version_id = cv.id AND cvi.meta_key = "M"');
$this->db->join($this->table_cmi.' cmi', 'cmi.id = cvi.meta_value');

我已经尝试过外连接,但是由于表 cvi 位于表 cvcmi 之间,因此没有公共(public)值两个表。

+------------+
| c |
| |
| +-----|------+
| | | cv |
+------------+ |
| +------------+
| | | cvi | When this table is empty, the result is empty
+-------|----+ | I want it to still show result of c and cv
| +------------+
| | | cmi |
+------------+ |
| |
| |
+------------+

这里举例说明了为什么没有共享值。因此,我正在寻找一种方法来创建一个条件,该条件仅在 cvi 包含值时才加入 cmi。否则只在 cvi 上进行外部连接并且不包含表 cmi

您能提供一些想法或解决方案吗?

编辑为了清楚起见,以下是表格:

/* Table c */
+------+--------+
| id | title |
+------+--------+

/* Table cv */
+------+----------+
| id | version |
+------+----------+

/* Table cvi */
+------+------------+-----------+------------+
| id | version_id | meta_key | meta_value |
+------+------------+-----------+------------+
/* when meta_key is 'M' then the meta_value will contain the cmi.id which is used for the join (regard it as meta_id) */
/* When this table is empty there won't be data in `cmi` either. When it's empty the join removes the data result that should be present from table `c`. */

/* Table cmi */
+------+-----------+------------+
| id | item_key | item_value |
+------+-----------+------------+

这是表cvcmi中有数据时的结果。

Array (
[0] => stdClass Object
(
[id] => 5 /* This is c.id */
[title] => Content title
[version_id] => 8 /* This is cv.id */
[version] => 0
[meta_key] => M
[meta_value] => 23 /* (This is the id of the item below, cmi.id) */
[item_key] => KEY1
[item_value] => Value
)
)

最佳答案

在我看来你必须采用左连接:

/* Tables */
$this->db->from($this->table_c.' c');
$this->db->join($this->table_cv.' cv', 'cv.content_id = c.id', 'inner');
$this->db->join($this->table_cvi.' cvi', 'cvi.version_id = cv.id AND cvi.meta_key = "M"', 'left');
$this->db->join($this->table_cmi.' cmi', 'cmi.id = cvi.meta_value', 'left');

因此,如果 cvi 和 cmi 的连接条件没有产生任何结果,您将得到空列。

在您发表评论后进行编辑:也许 EXISTS 有帮助:

$this->db->where("EXISTS(SELECT * FROM cmi)");

Codeigniter subquery exists

关于php - NULL 上的第三次连接不返回值(Codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44720720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com