2017-03-02 37 views
0

我需要幫助查詢兩條表成爲表加入表 - 行至

第一個表,Tbl_Project:

|No_Project  | Date_Start| Date_End | 
|---------------|---------- |-----------| 
|050/1/2015  | 20150105| 20150315 | 
|051/2/2015  | 20150205| 20150515 | 
|052/3/2015  | 20150305| 20150715 | 
|053/4/2015  | 20150405| 20150915 | 

第二個表,Tbl_DimDate:

| Date  | Year | Month| Day | Tanggal_Id | Weekth |Month_Name | 
| -----------| ------| ------| ------| -----------| ------ |-----------| 
| 2015-01-05 | 2015 |  1| 5 | 20150105|  2 | Jan | 
| 2015-02-05 | 2015 |  2| 5 | 20150205|  6 | Feb | 
| 2015-03-05 | 2015 |  3| 5 | 20150305| 10 | Mar | 
| 2015-04-05 | 2015 |  4| 5 | 20150405| 15 | Apr | 
| 2015-03-15 | 2015 |  3| 15 | 20150315| 12 | Mar | 
| 2015-05-15 | 2015 |  5| 15 | 20150515| 20 | May | 
| 2015-07-15 | 2015 |  7| 15 | 20150715| 29 | Jul | 
| 2015-09-15 | 2015 |  9| 15 | 20150915| 38 | Sept | 

如何使連接表看起來像這樣:

|No_Project | Date_Start| Date_End | Week_Start | Week_End | 
|-----------|---------- |-----------|------------|----------| 
|050/1/2015 | 20150105| 20150315 |  2 |  12 | 
|051/2/2015 | 20150205| 20150515 |  6 |  20 | 
|052/3/2015 | 20150305| 20150715 |  10 |  29 | 
|053/4/2015 | 20150405| 20150915 |  15 |  38 | 
+0

可能的複製[SQL JOIN和不同類型的連接(http://stackoverflow.com/questions/17946221/sql-join-and - 不同類型的連接) – MikaelF

回答

-1

您可以嘗試類似的措施:

SELECT `project`.*, `dimdate1`.`Weekth` as `Week_Start`, `dimdate2`.`Weekth` as `Week_End` FROM `Tbl_Project` as `project` INNER JOIN `Tbl_DimDate` as `dimdate1` 
INNER JOIN `Tbl_DimDate` as `dimdate2` 
WHERE `Date_Start` = `dimdate1`.`Tanggal_Id` AND `Date_End` = `dimdate2`.`Tanggal_Id` 
+0

這有錯誤,不會運行。 – Hogan

+0

@Hogan什麼是錯誤?我可以在MySQL 14.14 – Masoud

+0

上運行它...這是使用1995年以前的風格的連接 - 所以我錯過了閱讀。醜,但會工作。 – Hogan