2017-06-15 148 views
-1

我在單個數據庫中有多個表;從多個表中選擇具有相同列名的數據

UserAccount表; [UserAccount表] [1]

用戶表 [用戶表] [2]

EducationLevels表 EducationLevels table

機構表 Institution table

請注意,這兩個機構和EducationLevels表有相同的名字。

我想查詢數據庫使用;

SELECT 
     Users.FirstName, 
     Users.LastName, 
     UserAccounts.OtherNames, 
     UserAccounts.Gender, 
     UserAccounts.DateOfBirth, 
     Institutions.Name as School, 
     EducationLevels.Name as Study 
FROM 
     Users, UserAccounts,Institutions 
WHERE 
     UserAccounts.HighestEducationLevelId = EducationLevels.Id 
     AND 
     UserAccounts.InstitutionId = Institutions.Id 
     AND 
     UserAccounts.UserId = Users.IdUserAccounts 
     AND 
     MobileNumber ='*****'; 

我得到一個錯誤:

 Error Code: 1054. Unknown column 'EducationLevels.Name' in 'field 
    list' 
+0

您沒有在您的from子句 –

+0

中包含'EducationLevels',我在您的from子句中沒有看到任何EducationLevels表名。添加這樣的表名*從 用戶,UserAccounts,機構,EducationLevels * – JYoThI

回答

0

這是因爲「FROM」條款沒有表名。另外,爲表名添加別名,以便在整個查詢中使用它們更容易。

SELECT 
     usr.FirstName, 
     usr.LastName, 
     accounts.OtherNames, 
     accounts.Gender, 
     accounts.DateOfBirth, 
     inst.Name as School, 
     eduLevel.Name as Study 
FROM 
     Users usr, UserAccounts accounts, Institutions inst, EducationLevels eduLevel 
.... 
0

問題就在這裏:

FROM Users, UserAccounts,Institutions 

EducationLevels表是不存在的

你忘了在添加EducationLevels FROM表名上市,僅Users, UserAccounts,Institutions在那裏。

+0

謝謝你們......我真的應該看起來不錯。 – Edunge

相關問題