mySQL“未知列”错误

时间:2014-08-24 23:25:19

标签: mysql sql

  $queryChart ="SELECT CONCAT(u1.adGivenName,' ', u1.adSurName) AS EnteredBy, CONCAT(u2.adGivenName,' ', u2.adSurName) AS FixedBy, tblEquipIssues.Floor, tblEquipIssues.Location, tblEquipIssues.LocationNum, tblEquipIssues.EquipType, tblEquipIssues.UnitNo, tblEquipIssues.SendTo, tblEquipIssues.IssueDate, tblEquipIssues.RespondDate, tblEquipIssues.FixedDate, tblEquipIssues.CloseTicket
    FROM tblusers AS u1, tblusers AS u2 
    INNER JOIN tblEquipIssues
    ON (tblEquipIssues.EnteredBy = u1.userName) AND (tblEquipIssues.FixedBy = u2.userName)
    WHERE tblEquipIssues.Location LIKE '%$Location%' AND
      tblEquipIssues.LocationNum LIKE '%$LocationNum%' AND
      tblEquipIssues.EquipType LIKE '%$EquipType%' AND
      tblEquipIssues.UnitNo LIKE '%$EquipNum%' AND
      (tblEquipIssues.IssueDate >= '$FromIssueDate' OR '$FromIssueDate' = '') AND 
      (tblEquipIssues.IssueDate <= '$ToIssueDate' OR '$ToIssueDate' = '') AND
      (tblEquipIssues.FixedDate >= '$FromFixedDate' OR '$FromFixedDate' = '') AND 
      (tblEquipIssues.FixedDate <= '$ToFixedDate' OR '$ToFixedDate' = '') AND
      tblEquipIssues.EnteredBy LIKE '%$EnteredBy%' AND
      tblEquipIssues.tblEquipIssuesID LIKE '%$TicketNum%' AND
      tblEquipIssues.FixedBy LIKE '%$Assigned%'";

我收到&#34;未知栏&#39; u1.userName&#39;在&#39; on&#39;&#39;&#34;。当我在FROM子句中分配别名时,我不明白。我虽然对SQL很新。我确实在寻找答案,但我找到的答案似乎都没有帮助。

感谢任何建议。

提前感谢

2 个答案:

答案 0 :(得分:2)

之所以发生这种情况,是因为ON子句只能看到实际连接的两个表。使用此语法时:

SELECT ....
FROM tblusers AS u1, tblusers AS u2 
INNER JOIN tblEquipIssues
ON ....

ON子句中,您将能够引用来自u2和tblEquipIssues的列

您需要重写查询,以便所有表都加入:

SELECT CONCAT(u1.adGivenName,' ', u1.adSurName) AS EnteredBy, 
CONCAT(u2.adGivenName,' ', u2.adSurName) AS FixedBy, tblEquipIssues.Floor, 
tblEquipIssues.Location, tblEquipIssues.LocationNum, tblEquipIssues.EquipType, 
tblEquipIssues.UnitNo, tblEquipIssues.SendTo, tblEquipIssues.IssueDate, 
tblEquipIssues.RespondDate, tblEquipIssues.FixedDate, tblEquipIssues.CloseTicket
FROM tblEquipIssues
INNER JOIN tblusers AS u1 ON tblEquipIssues.EnteredBy = u1.userName
INNER JOIN tblusers AS u2 ON tblEquipIssues.FixedBy = u2.userName
WHERE tblEquipIssues.Location LIKE '%$Location%' AND
  tblEquipIssues.LocationNum LIKE '%$LocationNum%' AND
  tblEquipIssues.EquipType LIKE '%$EquipType%' AND
  tblEquipIssues.UnitNo LIKE '%$EquipNum%' AND
  (tblEquipIssues.IssueDate >= '$FromIssueDate' OR '$FromIssueDate' = '') AND 
  (tblEquipIssues.IssueDate <= '$ToIssueDate' OR '$ToIssueDate' = '') AND
  (tblEquipIssues.FixedDate >= '$FromFixedDate' OR '$FromFixedDate' = '') AND 
  (tblEquipIssues.FixedDate <= '$ToFixedDate' OR '$ToFixedDate' = '') AND
  tblEquipIssues.EnteredBy LIKE '%$EnteredBy%' AND
  tblEquipIssues.tblEquipIssuesID LIKE '%$TicketNum%' AND
  tblEquipIssues.FixedBy LIKE '%$Assigned%'

答案 1 :(得分:0)

我假设问题是你引用第二个表和On之间Join的{​​{1}}子句中的第一个表。

每张桌子都需要tblEquipIssues

Join