douzuizhuo0587 2016-03-23 15:54 采纳率: 0%
浏览 222

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


Codeigniter 3 running on a variant of PHP 5 server. Using the Query Builder to deal with the db.


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.


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.

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.

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.

Or any other way that would join the tables if there is a value and where it is NULL, then don't join.

Any help is appreciated!

  • 写回答



      相关推荐 更多相似问题


      • ¥15 实值特征权重向量和特征选择,多分类
      • ¥15 INVEST年产水量模块结果出处理
      • ¥15 Neo4j 不显示关系
      • ¥15 这段代码有什么bug
      • ¥15 pip默认安装位置修改不成功
      • ¥15 C++ inline、staitc、const三合一谁能讲通透
      • ¥15 关于pyqt5中QThread的问题
      • ¥20 STM32基于HAL库使用串口发送16进制数组
      • ¥15 OpenCV报错 You can enable it via 'OPENCV_IO_ENABLE_OPENEXR'
      • ¥15 AT89C51的程序问题