簡體   English   中英

如何檢查特定ID是否存在於另一個表中?

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

前端設計截圖我有一個名為 Team_members 的表,列是 team_id、staffid 和 stafftype..我正在從作業表中獲取數據。 從作業表中獲取時

如何檢查團隊成員表中是否沒有 Team_id(然后返回員工類型空字段而不是顯示未找到記錄)?? 或者第二個場景,如果 team_member 表中有 team_id 並檢查 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。有時數據不會存在

模態功能

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