从具有相同列名的多个表中选择数据

时间:2017-06-15 06:33:27

标签: mysql

我在一个数据库中有多个表;

UserAccount表; [UserAccount表] [1]

用户表 [用户表] [2]

EducationLevels表 EducationLevels table

机构表 Institution table

请注意,Institution和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'

2 个答案:

答案 0 :(得分: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
....

答案 1 :(得分:0)

问题在于:

FROM Users, UserAccounts,Institutions

EducationLevels表不存在

您忘记在FROM表名称列表中添加EducationLevels,只有Users, UserAccounts,Institutions