不知道为什么错误总是存在,内连接问题?

时间:2017-04-17 19:49:51

标签: sql postgresql

SELECT bookingid, customer.customerid, flightid, numseats, firstname, surname, billingadress, email
FROM flightbooking, leadcustoemr;
INNER JOIN customerid ON flighbooking.customerid = leadcustomer.customerid;

当我在PG Admin 4中运行时,我在“SELECT”或其附近得到语法错误,问题出在哪里,我认为它在内连接中,但不确定。

2 个答案:

答案 0 :(得分:0)

在leadcustomer之后你有一个分号(它本身似乎有一个拼写错误) - 结束你的select语句并且不包括内连接(此外,内连接没有使用正确的表名)。同样在select中,您正在使用customer表中的一列(并且该表根本没有连接)。

我认为您正在尝试使用Inner Join加入flightbooking和leadcustomer表,而不是实际尝试添加第三个表。我在这里编辑了你的查询:

SELECT bookingid, flightbooking.customerid, flightid, numseats,
   firstname, surname, billingadress, email
FROM flightbooking
INNER JOIN leadcustomer ON (flightbooking.customerid = leadcustomer.customerid);

答案 1 :(得分:0)

join的语法有两种形式:https://www.postgresql.org/docs/current/static/tutorial-join.html

SELECT *
FROM A, B
WHERE A.id = B.id;

所以根据上面的语法重写查询:

 SELECT bookingid, customer.customerid, flightid, numseats, firstname, 
 surname, billingadress, email
 FROM flightbooking, leadcustomer
 WHERE flightbooking.customerid= leadcustomer.customerid

更多标准语法是:

SELECT *
FROM A
INNER JOIN B
ON A.id = B.id

因此,重写查询会导致:

SELECT bookingid, customer.customerid, flightid, numseats, firstname, surname, 
billingadress, email
FROM flightbooking
INNER JOIN  leadcustomer 
ON flighbooking.customerid = leadcustomer.customerid;`

你可以使用别名来缩短它:

SELECT bookingid, customer.customerid, flightid, numseats, firstname, surname, 
billingadress, email
FROM flightbooking fb
INNER JOIN  leadcustomer  lc
ON fb.customerid = lc.customerid;