如何连接表格

时间:2019-05-28 18:25:43

标签: sqlite

我坚持使用我的代码。我希望我的serviceticket表中的所有内容都显示为CUSTID。我收到一个错误“列名不明确:CUSTID” 谁能帮我?我已经尝试了一切

create table customers(CUSTID INTEGER PRIMARY KEY AUTOINCREMENT,
    FIRSTNAME TEXT(50),
    LASTNAME TEXT (50),
    STREET TEXT(50),
    CITY TEXT(50),
    ZIP INT(4),
    PHONENUMBER INT(8),
    EMAIL VARCHAR(50)
);

create table serviceticket(SERVICETICKETID int(2) NOT NULL PRIMARY KEY,
    TICKETNUMBER INT,
    CUSTID int,
    DATE_RECIEVED text,
    EXPECTED_DELIEVERYDATe TEXT,
    COMMENTS TEXT,
    DATE_RETURNED TEXT
);

insert into customers (FIRSTNAME, LASTNAME, STREET, CITY, ZIP, PHONENUMBER, EMAIL) 
  values ('Jørgen', 'Marven', 'Dræmmestad 22', 'Gjøvik', 6026,12837262, 'jørge@gmail.com');

insert into serviceticket(SERVICETICKETID, TICKETNUMBER,CUSTID, DATE_RECIEVED, EXPECTED_DELIEVERYDATE, COMMENTS, DATE_RETURNED)
  values (56, 133, NULL, NULL, '12/4/19', '22/4/19', 'Wants authenic parts', '22/4/19');


select SERVICETICKETID, TICKETNUMBER, CUSTID, DATE_RECIEVED, EXPECTED_DELIEVERYDATE, COMMENTS, DATE_RETURNED
from serviceticket, customers
where serviceticket.CUSTID = customers.CUSTID;

1 个答案:

答案 0 :(得分:0)

您在写select SERVICETICKETID, TICKETNUMBER, CUSTID, ...时未指定CUSTID是来自serviceticket还是customers。写

select SERVICETICKETID, TICKETNUMBER, customers.CUSTID, ...

,并且应该使用比where语法更新的join语法来连接表。您还可以使用表别名使查询更具可读性

select
    S.SERVICETICKETID, S.TICKETNUMBER, C.CUSTID, S.DATE_RECIEVED, S.EXPECTED_DELIEVERYDATE,
    S.COMMENTS, S.DATE_RETURNED
from
    serviceticket S
    INNER JOIN customers C
        ON S.CUSTID = C.CUSTID