2016-08-14 93 views
0

表名:资源编写查询以特定的方式来获取数据

res_id | first_name 
    1  clyde 
    2  John 
    3  Rachel 
    4  Jason 
    5  Rasmon 
    6  Pierson 
    7  Jamson 
    8  Radamel 

表名:probation_hr

probation_id | res_id | month_id | hr_id 
    1    1   M1  4 
    2    1   M2  4 
    3    2   M1  4 
    4    2   M2  4 

表名:probation_feedback

prob_id | reviewer_id | res_id 
    1   6   null 
    1   7   null 
    1   8   null 
    1   null  1 

RES_ID在不为空缓刑反馈表意味着缓刑犯已经填补了他的反馈意见

给定一组RESOURCE_ID的,我希望获取他的细节如下

resources.first_name, 
probation_hr.month_id, 
probation_hr.probation_id, 
probation_hr.hr_id (also get this id's first name from resources table), 
and lastly get all records from probation_feedback table where 
probation_hr.probation_id = probation_feedback.probation_id (here there are multiple records, so do i use another array here?) 

单个示例情况: RESOURCE_ID = 1 输出: 克莱德(resources.first_name),4(probation_hr.hr_id), M1(probation_hr.month_id),1(probation_hr.probation_id),阵列(从probation_feedback表,其中probation_id = 1的记录):

1   6   null, 
1   7   null 
1   8   null 
1   null  1 

我如何做到这一点的一个查询?

是否有可能?

如果不可能比我怎么做呢? 我使用2d数组吗?

即时通讯使用PHP和MySQL

thanx提前

+1

什么?你能否将你的问题归结为可以理解的主要问题?并格式化代码/表格defs,以便从评论中分离出来? (点击右上角的'?'查看如何) – Jeff

回答

0

嗯,我没有足够的信誉发表评论,让我冒充一个答案。您将使用连接从上述表格中获得结果。请找到下面的查询得到的结果:

SELECT r.first_anme 
    , p_h.month_id 
    , p_h.probation_id 
    , p_h.hr_id 
    , p_f.* 
    FROM probation_feedback p_f 
    LEFT 
    JOIN probation_hr p_h 
    ON p_f.prob_id = p_h.probation_id 
    LEFT 
    JOIN resources r 
    ON r.res_id = p_h.res_id; 

在这里,你会得到多个结果,因为probation_feedback表具有相同的组成多个prob_id记录。我不认为你会从probation_feedback表中得到记录作为数组,但是你会得到多行结果。让我知道,如果它有效或不。谢谢

相关问题