链接查询错误代码1052

时间:2013-03-25 09:12:08

标签: mysql-error-1052

我收到错误代码1052当在查询中将两个表等同起来时,字段列表中的FireStationHome列不明确

这是我在下面使用的代码:

SELECT FiremanID, FiremanHomeStation,StationPhone
FROM FIRE_REPORT AS F, FIRE_STATION AS FS
WHERE F.FiremanHomeStation = FS.FiremanHomeStation
AND StationPhone ='703';

从阅读论坛我了解到我需要通过链接键链接第一个关系。 FiremanHomeStation是我需要运行以下指示的唯一链接密钥。

为所有以“703”开头的消防员显示FiremanID,FiremanHomeStation和电台号码。

使用Boyce Codd方法我已将以下内容标准化

FIRE_REPORT(FireID,Alarms,Address,FiremanID,FiremanName,FiremanPhone,FiremanHomeStation,StationAddress,StationPhone,TruckID,LicensePlate)

规则:

  

每个火灾可以分配几名消防员。每次火灾   可以有几辆消防车分配给它每个消防员都可以   被分配到几个火灾
每辆消防车并被分配到   几次火灾每个消防员都有一个家庭站   电台是唯一的 - 每个电台都有一个电话号码

主键

FireID, FiremanID, FiremanHomeStation,TruckID

FireID -> Alarms, Address
FiremanID -> FiremanName, FiremanPhone, FiremanHomeStation
FiremanHomeStation -> StationAddress, StationPhone
TruckID -> LicensePlate

FIRE_REPORT( FireID,FiremanID,FiremanHomeStation,TruckID
FIRE( FireID ,警报,地址)
FIREMAN_ID( FiremanID ,FiremanName,FiremanPhone)
FIRE_STATION( FiremanHomeStation ,StationAddress,StationPhone)
卡车( TruckID ,LicensePlate)

参照完整性约束
FIRE_REPORT中的FireID必须存在于FIRE中的FireID中 FIRE_REPORT中的FiremanID必须存在于FIREMAN_ID中的FiremanID中 FIRE_REPORT中的FiremanHomeStation必须存在于FIRE_STATION中的FiremanHomeStation中 FIRE_REPORT中的TruckID必须存在于TRUCK中的TruckID中

create schema project_george;<br>
use project_george;<br>

CREATE TABLE FIRE   (<br>
    FireID int(3) NOT NULL,<br>
    Alarms int(1) NOT NULL,<br>
    Address varChar(25) NOT NULL,<br>
        CONSTRAINT FIRE_PK PRIMARY KEY(FireID),<br>
        CONSTRAINT FIRE_FR_FK FOREIGN KEY(FireID)<br>
        REFERENCES FIRE_REPORT(FireID)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE FIREMAN_ID (<br>
    FiremanID int(3) NOT NULL,<br>
    FiremanName varchar(25) NOT NULL,<br>
    FiremanPhone char(10) NOT NULL UNIQUE,<br>
        CONSTRAINT FI_PK PRIMARY KEY(FiremanID),<br>
        CONSTRAINT FI_FR_FK FOREIGN KEY(FiremanID)<br>
        REFERENCES FIRE_REPORT(FiremanID)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE FIRE_STATION   (<br>
    FiremanHomeStation varChar(25) NOT NULL UNIQUE,<br>
    StationAddress varChar(25) NOT NULL,<br>
    StationPhone char(10) NOT NULL,<br>
        CONSTRAINT FS_PK PRIMARY KEY(FiremanHomeStation),<br>
        CONSTRAINT FS_FR_FK FOREIGN KEY(FiremanHomeStation)<br>
        REFERENCES FIRE_REPORT(FiremanHomeStation)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE TRUCK  (<br>
    TruckID int(3) NOT NULL,<br>
    LicensePlate char(5) NOT NULL UNIQUE,<br>
            CONSTRAINT TRUCK_PK PRIMARY KEY(TruckID),<br>
            CONSTRAINT TRUCK_RC_FK FOREIGN KEY(TruckID)<br>
            REFERENCES FIRE_REPORT(TruckID)<br>
            ON UPDATE CASCADE<br>
);<br>

CREATE TABLE FIRE_REPORT    (<br>
    FireID  int(3)  NOT NULL,<br>
    FiremanID int(3) NOT NULL,<br>
    FiremanHomeStation varChar(25) NOT NULL,<br>
    TruckID int(3) NOT NULL,<br>
        CONSTRAINT FR_PK PRIMARY KEY(FireID,FiremanID,FiremanHomeStation,TruckID),<br>
        CONSTRAINT FR_F_FK FOREIGN KEY (FireID)<br>
        REFERENCES FIRE(FireID) <br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_FID_FK FOREIGN KEY(FiremanID)<br>
        REFERENCES FIREMAN_ID(FiremanID)<br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_FS_FK FOREIGN KEY(FiremanHomeStation)<br>
        REFERENCES FIRE_STATION(FiremanHomeStation)<br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_TRUCK_FK FOREIGN KEY(TruckID)<br>
        REFERENCES TRUCK(TruckID)<br>
            ON UPDATE CASCADE<br>
);<br><br>

INSERT INTO FIRE VALUES(100, 1, '4025 Medford dr, Annandale VA 22003');<br>
INSERT INTO FIRE VALUES(101, 2, '7031 Little River Turnpike Suite 5A
Annandale, VA 22003-5958');<br>
INSERT INTO FIRE VALUES(102, 3, '7001 Little River Turnpike, Annandale, VA 22003 ');<br>
INSERT INTO FIRE VALUES(103, 4, '8333 Little River Turnpike Annandale, VA 22003');<br>
INSERT INTO FIRE VALUES(104, 5, '1600 Pennsylvania Ave NW  Washington, DC 20500');<br><br>

INSERT INTO FIREMAN_ID VALUES(100, 'Guliza Ormuzokova', '703-221-9918');<br>
INSERT INTO FIREMAN_ID VALUES(101, 'Lex Steele', '703-911-4450');<br>
INSERT INTO FIREMAN_ID(FiremanID,FiremanName,FiremanPhone) VALUES(102, 'George Putnam', '703-222-9918');<br>
INSERT INTO FIREMAN_ID VALUES(103, 'Steven Drasner', '571-939-1234');<br>
INSERT INTO FIREMAN_ID VALUES(104, 'Judy Miller', '571-999-1234');<br>
INSERT INTO FIREMAN_ID VALUES(105, 'Lily Thai', '703-911-4451');<br><br>

INSERT INTO FIRE_STATION VALUES('Station 29', '571 Broad st
Fairfax VA 22030', '703-200-9918');<br>
INSERT INTO FIRE_STATION VALUES('Station 229', '8914 Little River Turnpike, Fairfax, VA 22031 ', '703-900-4450');<br>
INSERT INTO FIRE_STATION VALUES('Station 429', '3988 University Dr, Fairfax, VA 22030 ', '571-900-1234');<br><br>

INSERT INTO TRUCK VALUES(1, 'burn29');<br>
INSERT INTO TRUCK VALUES(2, 'burn19');<br>
INSERT INTO TRUCK VALUES(3, 'burn09');<br><br>

/************I can't figure this on out!!!*** *****/<br>
SELECT FiremanID, FiremanHomeStation,StationPhone<br>
FROM FIRE_REPORT AS F, FIRE_STATION AS FS<br>
WHERE F.FiremanHomeStation = FS.FiremanHomeStation<br>
AND StationPhone ='703';<br>

1 个答案:

答案 0 :(得分:0)

FiremanHomeStation既存在于两个表中,但您没有指定要从哪个表中获得结果,这是不明确的。

SELECT FiremanID, FS.FiremanHomeStation,StationPhone
FROM FIRE_REPORT AS F, FIRE_STATION AS FS
WHERE F.FiremanHomeStation = FS.FiremanHomeStation
AND StationPhone ='703';

应该工作