简体   繁体   English

PHP Codeigniter-如果列不等于NULL,则加入表

[英]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.

相关问题 表的列总和使用左连接返回null(codeigniter) - sum of table's column return null (codeigniter) using left join PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组 - PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table 如何在codeigniter中更新列设置为NULL的表 - How to update table with column set NULL in codeigniter 将列从一个表连接到另一个表CodeIgniter中的另一列 - Join column from one table to another column in other table CodeIgniter 图像列为空,如果不更新图像代码点火器 - Image column null if not update image codeigniter Php 选择具有多个联接和条件的列,其中null为null - select with multiple join and conditions where column null codeigniter Codeigniter Join 如果值不为 NULL - Codeigniter Join If the value is not NULL 与空表的左连接使在 php 中的 null 上连接的列,但在 phpmyadmin 中正常工作 - Left join with empty table makes the column that is joined on null in php, but works normally in phpmyadmin CodeIgniter或Simple PHP,如何将一个表与另一个表的两个列连接在一起? - CodeIgniter or Simple PHP , how to join a table with two colomns of another table? 加入表Codeigniter时字段列表中的列不明确 - Ambigous column in field list when join table codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM