简体   繁体   English

如何检查特定ID是否存在于另一个表中?

[英]How to Check specific ID is exists in another table?

Frontend design screenshot I have a table named Team_members, columns are team_id, staffid and stafftype..I am fetching datas from job table.前端设计截图我有一个名为 Team_members 的表,列是 team_id、staffid 和 stafftype..我正在从作业表中获取数据。 While fetching from job table从作业表中获取时

how to check if Team_id is not there in team member table (then return staff type empty field instead of showing No record found)??如何检查团队成员表中是否没有 Team_id(然后返回员工类型空字段而不是显示未找到记录)?? or second senario if team_id is there in team_member table and check Leader(stafftype) is there belongs to that team_id.或者第二个场景,如果 team_member 表中有 team_id 并检查 Leader(stafftype) 是否属于该 team_id。

Team table looks like团队表看起来像

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

I need to fetch all the columns from the job table..In this am fetching staff name from stafftable.我需要从工作表中获取所有列..在这个我从员工表中获取员工姓名。 Before that i need to check team_id is exists in the Team member table.Sometime data wont be there在此之前,我需要检查团队成员表中是否存在 team_id。有时数据不会存在

MODAL FUNCTION模态功能

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';
        }
    }
SELECT COUNT(Team_id) AS COUNTA FROM `Team Member` WHERE team_id=x;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM