是否有更简单的方法来执行此类查询

时间:2013-11-20 19:45:26

标签: mysql database join left-join inner-join

我花了大部分时间创造这个以最终得到我正在寻找的结果。我不熟悉SQL,但必须有一种更简单的方法来实现这一点。

查询

SELECT *, 
   Date(review_date 
        + INTERVAL review_period_weeks week) AS next_review_date 
FROM   (SELECT * 
    FROM   (SELECT tbl_candidate_temp.id, 
                   tbl_candidate_temp.last_name, 
                   tbl_candidate_temp.first_name, 
                   tbl_candidate_temp.ni_number, 
                   tbl_candidate_temp.date_of_birth, 
                   tbl_candidate_temp.start_date, 
                   tbl_candidate_temp.expected_end_date, 
                   tam_company_main.id   AS company_id, 
                   tam_company_main.name AS company_name, 
                   review_period_weeks 
            FROM   (SELECT tam_candidate.id, 
                           tam_candidate.last_name, 
                           tam_candidate.first_name, 
                           tam_candidate.ni_number, 
                           tam_candidate.date_of_birth, 
                           tam_candidate.start_date, 
                           tam_candidate.expected_end_date, 
                           tam_candidate.company_main_lu, 
                           review_period_weeks 
                    FROM   tam_candidate 
                           INNER JOIN (SELECT tam_candidate.id 
                                              AS 
                                              id, 
                                              tam_country.name 
                                              AS 
                                              award_country_name, 
tam_country.review_period_weeks AS 
review_period_weeks 
FROM   tam_candidate 
INNER JOIN tam_country 
    ON 
tam_candidate.award_country_lu = 
tam_country.id 
WHERE  reviewer = 1) AS 
tbl_candidate 
ON tam_candidate.id = tbl_candidate.id) AS 
tbl_candidate_temp 
INNER JOIN tam_company_main 
ON 
tbl_candidate_temp.company_main_lu = tam_company_main.id) 
AS tbl_candidate_temp2 
LEFT JOIN (SELECT tam_candidate_review.candidate_lu, 
review_date 
FROM   tam_candidate_review 
ORDER  BY review_date DESC) AS tbl_review_temp 
ON tbl_candidate_temp2.id = tbl_review_temp.candidate_lu) AS 
tbl_candidate_temp3 

候选表包含主要数据,例如名称,NI编号等

该国家/地区包含县名,以及必须在该国家/地区进行审核的周数

公司表包含公司名称

评论表包含日期和候选人编号

reviwer是当前登录审阅者的ID。

我的目标是拥有以下列:

id < candidate id
last_name
first_name
ni_number
date_of_birth
start_date
expected_end_date
company_id 
company_name<lookup from company table 
review_period_weeks<lookup from country
candidate_lu
review_date < last review 
next_review_date < last review + review_period_weeks WEEKS

0 个答案:

没有答案