2017-07-25 62 views
0

我從一些表中獲得價值的問題。你可以看到下面的圖片,我想用紅色來排列我所阻擋的東西。 enter image description here從INNER JOIN獲取價值3表中每個表的最新記錄

我嘗試用下面的代碼

SELECT p.id, 
     p.email, 
     p.name, 
     p.lastname, 
     p.gender, 
     ex.startwork, 
     ex.endwork, 
     e.degree, 
     e.majority, 
     j.division 
FROM job_jobseeker AS p 
     INNER JOIN job_experience AS ex 
       ON p.email = (SELECT ex.email 
          FROM job_experience 
          ORDER BY ex.id DESC 
          LIMIT 1) 
     INNER JOIN job_education AS e 
       ON p.email = (SELECT e.email 
          FROM job_education 
          ORDER BY ex.id DESC 
          LIMIT 1) 
     INNER JOIN job_applying AS j 
       ON p.email = (SELECT j.email 
          FROM job_applying 
          ORDER BY ex.id DESC 
          LIMIT 1) 

回答

1

你需要correlated sub-queries

找到每個email最新id所有三個表

SELECT startwork, 
     endwork, 
     email 
FROM job_experience a 
WHERE a.id = (SELECT Max(b.id) 
       FROM job_experience b 
       WHERE a.email = b.email) 

上面的查詢中找到每個郵件最新的ID在job_experience表。對其他兩個表格也做同樣的事情,然後將結果與job_jobseeker表格結合起來得到結果。

SELECT p.id, 
     p.email, 
     p.name, 
     p.lastname, 
     p.gender, 
     ex.startwork, 
     ex.endwork, 
     e.degree, 
     e.majority, 
     j.division 
FROM job_jobseeker AS p 
     INNER JOIN (SELECT startwork, 
          endwork, 
          email 
        FROM job_experience a 
        WHERE a.id = (SELECT Max(b.id) FROM job_experience b 
            WHERE a.email = b.email)) AS ex 
       ON p.email = ex.email 
     INNER JOIN (SELECT email, //Just called column without initialize 
          degree, 
          majority 
        FROM job_education a 
        WHERE a.id = (SELECT Max(b.id) FROM job_education b 
            WHERE a.email = b.email)) AS e 
       ON p.email = e.email 
     INNER JOIN (SELECT email, //Just called column without initialize 
          division 
        FROM job_applying a 
        WHERE a.id = (SELECT Max(b.id) FROM job_applying b 
            WHERE a.email = b.email)) AS j 
       ON p.email = j.email 
+0

我編輯了一些您的代碼,但它的工作原理...感謝您的高級 – bramadwitra