简体   繁体   English

SQL-字段列表中的列不明确

[英]SQL - Column in field list is ambiguous

I have two tables BOOKINGS and WORKER. 我有两个表BOOKINGS和WORKER。 Basically there is table for a worker and a table to keep track of what the worker has to do in a time frame aka booking. 基本上,这里有一个供员工使用的桌子和一个桌子,以记录该员工在预定时间内的工作。 I'm trying to check if there is an available worker for a job, so I query the booking to check if requested time has available workers between the start end date. 我正在尝试检查是否有空缺职位,因此我查询预订以检查在开始结束日期之间请求的时间是否有空缺职位。 However, I get stuck on the next part. 但是,我会停留在下一部分。 Which is returning the list of workers that do have that time available. 这将返回确实有该时间的工作人员列表。 I read that I could join the table passed on a shared column, so I tried doing an inner join with the WORKER_NAME column, but when I try to do this I get a ambiguous error. 我读到我可以加入通过共享列传递的表,所以我尝试使用WORKER_NAME列进行内部联接,但是当我尝试执行此操作时,我得到了一个模棱两可的错误。 This leads me to believe I misunderstood the concept. 这使我相信我误解了这个概念。 Does anyone understand what I;m trying to do and knows how to do it, or knows why I have the error below. 是否有人了解我正在尝试做的事情,并且知道该怎么做,或者知道我为什么在下面出现错误。 Thanks guys !!!! 多谢你们 !!!!

CREATE TABLE WORKER (
            ID INT NOT NULL AUTO_INCREMENT,
            WORKER_NAME  varchar(80) NOT NULL,
            WORKER_CODE  INT, 
            WORKER_WAGE  INT, 
            PRIMARY KEY (ID)
)

CREATE TABLE BOOKING (
            ID INT NOT NULL AUTO_INCREMENT,
            WORKER_NAME  varchar(80) NOT NULL,
            START DATE NOT NULL,
            END DATE NOT NULL, 
            PRIMARY KEY (ID)
)

query 询问

SELECT *
FROM WORKERS
INNER JOIN BOOKING
ON WORKER_NAME = WORKER_NAME
WHERE (START NOT BETWEEN '2010-10-01' AND '2010-10-10')
ORDER BY ID

#1052 - Column 'WORKER_NAME' in on clause is ambiguous #1052-on子句中的列“ WORKER_NAME”不明确

In your query, the column "worker_name" exists in two tables; 在您的查询中,“ worker_name”列存在于两个表中。 in this case, you must reference the tablename as part of the column identifer. 在这种情况下,您必须引用表名作为列标识符的一部分。

SELECT *
FROM WORKERS
INNER JOIN BOOKING
ON workers.WORKER_NAME = booking.WORKER_NAME
WHERE (START NOT BETWEEN '2010-10-01' AND '2010-10-10')
ORDER BY ID

In your query, the column WORKER_NAME and ID columns exists in both tables, where WORKER_NAME retains the same meaning and ID is re-purposed; 在您的查询中,两个表中都存在WORKER_NAME列和ID列,其中WORKER_NAME保留相同的含义,并且ID被重新使用; in this case, you must either specify you are using WORKER_NAME as the join search condition or 'project away' (rename or omit) the duplicate ID problem. 在这种情况下,您必须指定使用WORKER_NAME作为WORKER_NAME搜索条件,或者“投影”(重命名或忽略)重复ID问题。

Because the ID columns are AUTO_INCREMENT , I assume (hope!) they have no business meaning. 因为ID列是AUTO_INCREMENT ,所以我认为(希望!)它们没有业务意义。 Therefore, they could both be omitted, allowing a natural join that will cause duplicate columns to be 'projected away'. 因此,它们都可以被省略,从而允许自然连接,从而导致重复的列被“投影掉”。 This is one of those situations where one wishes SQL had a WORKER ( ALL BUT ( ID ) ) type syntax; 这是希望SQL具有WORKER ( ALL BUT ( ID ) )类型语法的情况之一; instead, one is required to do it longhand. 相反,需要长期这样做。 It might be easier in the long run to to opt for a consistent naming convention and rename the columns to WORKER_ID and BOOKING_ID respectively. 从长远来看,选择一致的命名约定并将列分别重命名为WORKER_IDBOOKING_ID可能会更容易。

You would also need to identify a business key to order on eg ( START, WORKER_NAME ) : 您还需要标识要订购的业务密钥,例如( START, WORKER_NAME )

  SELECT *
  FROM
  ( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
  NATURAL JOIN
  ( SELECT WORKER_NAME, START, END FROM BOOKING ) AS B
  WHERE ( START NOT BETWEEN '2010-10-01' AND '2010-10-10' )
  ORDER BY START, WORKER_NAME;

This is good, but its returning the start and end times as well. 这很好,但是它也返回开始时间和结束时间。 I'm just wanting the WOKER ROWS. 我只想要WOKER ROWS。 I cant take the start and end out, because then sql doesn't recognize the where clause. 我无法开始和结束,因为sql无法识别where子句。

Two approaches spring to mind: push the where clause to the subquery: 有两种方法可供考虑:将where子句推送到子查询:

  SELECT *
  FROM
  ( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
  NATURAL JOIN
  ( SELECT WORKER_NAME, START, END
      FROM BOOKING
     WHERE START NOT BETWEEN '2010-10-01' AND '2010-10-10' ) AS B
  ORDER BY START, WORKER_NAME;

Alternatively, replace SELECT * with a list of columns you want to SELECT : 或者,将SELECT *替换为要SELECT的列的列表:

  SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE
  FROM
  ( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
  NATURAL JOIN
  ( SELECT WORKER_NAME, START, END FROM BOOKING ) AS B
  WHERE START NOT BETWEEN '2010-10-01' AND '2010-10-10'
  ORDER BY START, WORKER_NAME;

This error comes after you attempt to call a field which exists in both tables, therefore you should make a reference. 尝试调用两个表中都存在的字段后,将出现此错误,因此应进行引用。 For instance in example below I first say cod.coordinator so that DBMS know which coordinator I want 例如,在下面的示例中,我首先说cod.coordinator以便DBMS知道我想要哪个协调器。

SELECT project__number, surname, firstname,cod.coordinator FROM coordinators AS co JOIN hub_applicants AS ap ON co.project__number = ap.project_id JOIN coordinator_duties AS cod ON co.coordinator = cod.email SELECT project__number, surname, firstname,cod.coordinator FROM协调员AS co JOIN hub_applicants AS ap ON co.project__number = ap.project_id JOIN coordinator_duties AS cod ON co.coordinator = cod.email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM