[英]PHP Codeigniter - Join a table if column does not equal NULL
Setup: 设定:
Codeigniter 3 running on a variant of PHP 5 server. 在PHP 5服务器的变体上运行的Codeigniter 3。 Using the Query Builder to deal with the db.
使用查询生成器来处理数据库。
Background: 背景:
Creating a software that has a client profile. 创建具有客户端配置文件的软件。 Users can add multiple notes to the profile, and link a contact to that note.
用户可以将多个注释添加到个人资料,并将联系人链接到该注释。 The profile then displays all the notes and the contact on the corresponding profile, using the
join()
method from Codeigniter 3's Query Builder. 然后,该配置文件使用Codeigniter 3的查询生成器中的
join()
方法在相应的配置文件上显示所有注释和联系人。
Issue: 问题:
A note can be added without a client contact, in the case of a generalised note. 对于一般性注释,可以在没有客户联系的情况下添加注释。 This sets the default value of
NULL
in the DB which in turn prevents the client_model from returning the notes, because it cant join the tables. 这将在数据库中设置
NULL
的默认值,这又阻止了client_model返回注释,因为它无法连接表。
Current code: 当前代码:
function get_client_notes($client_id)
{
$this->db->join('nh_note_types', 'nh_note_types.type_id = nh_client_notes.client_notes_type');
$this->db->join('nh_user_profiles', 'nh_user_profiles.user_profile_user_id = nh_client_notes.client_notes_added_by');
$this->db->join('nh_client_contacts', 'nh_client_contacts.client_contact_id = nh_client_notes.client_notes_client_contact_id');
$this->db->order_by("client_notes_added_date", "desc");
$query = $this->db->get_where('nh_client_notes', array('client_notes_client_id' => $client_id));
return $query;
}
Currently if the value for client_notes_client_contact_id
is NULL
it will not return any data for that row. 当前,如果
client_notes_client_contact_id
值为NULL
,则不会返回该行的任何数据。
What I am trying to find out is if there is a way to do the following: IF the client_notes_client_contact_id
is not null then join the tables, else carry on past. 我试图找出的是是否有一种方法可以执行以下操作:如果
client_notes_client_contact_id
不为null,则加入表,否则继续进行。
Or any other way that would join the tables if there is a value and where it is NULL
, then don't join. 否则,如果有值且为
NULL
,则将以其他任何方式联接表,则不要联接。
Any help is appreciated! 任何帮助表示赞赏!
Your current MySQL query with the above Query builder would 'build' like this: 使用上述查询构建器的当前 MySQL查询将像这样“构建”:
SELECT
*
FROM
nh_client_notes
JOIN nh_note_types ON
( nh_note_types.type_id = nh_client_notes.client_notes_type )
JOIN nh_user_profiles ON
( nh_user_profiles.user_profile_user_id = nh_client_notes.client_notes_added_by )
JOIN nh_client_contacts ON
( nh_client_contacts.client_contact_id
=
nh_client_notes.client_notes_client_contact_id
)
WHERE
client_notes_client_id = 479
ORDER BY
client_notes_added_date DESC
However, this will require all the JOINs to have an matching ID Available. 但是,这将要求所有JOIN具有匹配的可用ID。 Thus the correct MySQL would be
LEFT JOIN
on client_notes_client_contact_id
key, which is as you've requested to be conditional . 因此,正确的MySQL将为
client_notes_client_contact_id
键上的LEFT JOIN
,这是您要求的有条件的 。
In this instance, adjust your Query builder to have a third parameter on the join()
to make this 'left'. 在这种情况下,调整您的“查询”构建器,使其在
join()
上具有第三个参数,以使其成为“左”。
<?php
$this->db->join(
'nh_client_contacts',
'nh_client_contacts.client_contact_id = nh_client_notes.client_notes_client_contact_id',
'left'
);
?>
In doing so, this'll correct your query to bring back client_notes regardless of client_notes_client_contact_id
. 这样,这将更正您的查询,以使其不带client_notes_client_contact_id都带回
client_notes_client_contact_id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.