【问题标题】:How to Check specific ID is exists in another table?如何检查特定 ID 是否存在于另一个表中?
【发布时间】:2019-08-19 19:29:05
【问题描述】:

Frontend design screenshot 我有一个名为 Team_members 的表,列是 team_id、staffid 和 stafftype..我正在从作业表中获取数据。从作业表中获取时

如何检查团队成员表中是否没有 Team_id(然后返回人员类型空字段而不是显示未找到记录)?或第二个场景,如果 team_id 存在于 team_member 表中,并检查 Leader(stafftype) 是否属于该 team_id。

团队表的样子

team_id team_name 
1        xyz
2        yux
3        iop

**Team Member table looks like**

team_id staff_id staff_type
1          13      Leader
1          14      Technician
2          11      Leader
// 1. ASSUME 3 TEAMID IS NOT THERE IN TEAM MEMBERS TABLE
// 2. Assume 3 teamid is there,and check 3 have Leader .Sometime 3 have other stafftype called "technican" 


**Staff table looks like**

staff_id staffname
13         abc
14         tyy
15         fdg

**Job table looks like**

job_id jobdate     starttime   team_id
1      12-09-2018               1
2      12-09-2019               3
3      12-09-2018               1

我需要从工作表中获取所有列。这是从员工表中获取员工姓名。在此之前我需要检查 Team_id 是否存在于 Team member 表中。有时数据不会存在

模态函数

public function list_job_by_asignedteam($getselectedteam){ //value 3

        $this->db->select('*');
        $this->db->select('j.Start_time,
    LTRIM(RIGHT(CONVERT(VARCHAR(20), j.Start_time, 100), 7)) AS PeriodStarttime');
        $this->db->select('j.created_Dt as created_Dt_job, 
                            j.status as jobstatus,
                             mtos.created_Dt as created_Dt_mtos, 
                             mtos.type_of_services as type_of_services_name,
                             c.created_Dt as created_Dt_contract, 
                             aa.Contact_name as activity_contactname, 
                             aa.Location_name as Location_name_activityarea,
                             t.team_name as team_names,
                             aa.created_Dt as created_Dt_aa', false);
        $this->db->from('job j');
        $this->db->join('mt_type_of_services mtos', 'j.type_of_services_id = mtos.type_of_service_id');
        $this->db->join('contract c', 'j.Contract_id = c.Contract_id');
        $this->db->join('mt_business_type mbt', 'c.business_type_id = mbt.business_type_id');
        $this->db->join('activity_area aa', 'j.Activity_Area_id = aa.Activity_Area_id');
        $this->db->join('team t', 'j.team_id = t.team_id');

        //joining team_member table, staff table
        $this->db->join('team_members tm','t.team_id = tm.team_id');
        $this->db->join('staff s','tm.Staff_id = s.Staff_id');
        $this->db->where('tm.Staff_type', "Leader");


        $this->db->where("j.team_id",$getselectedteam); //value 3

        $this->db->order_by('Job_id', 'Desc');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return 'No Records Found';
        }
    }

【问题讨论】:

  • 三队没有成员怎么会有工作?还是您正在处理不完整的数据?
  • @csabinho 仅使用团队名称我已经创建了一个工作..如果假设用户创建了团队名称,那么可以添加一个工作..
  • 所以在从工作中获取数据时,我需要加入 team_members 表并检查 team_id 是否存在,或者如果存在则意味着需要检查 Leader 是否存在..除了 Leader 员工类型之外的其他时间会有技术人员喜欢那
  • 团队成员将位于该特定团队名称的另一个表中
  • @csabinho 你能回答我吗

标签: php mysql codeigniter codeigniter-3


【解决方案1】:
SELECT COUNT(Team_id) AS COUNTA FROM `Team Member` WHERE team_id=x;

如果 COUNTA 值为 0,那么显然您要搜索的记录不存在。

【讨论】:

  • 我猜他想做OUTER JOIN之类的事情。
  • @csabinho 我已经更新了我的模态函数。在此代码中,如果 team_id 在 team_members 表中不存在,则它显示 NO RECORDs FOUND. 而不是我需要显示员工姓名空字段..我是从作业表中获取数据
  • ChrisFNZ 有什么想法吗?
猜你喜欢
  • 2018-10-23
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多