postgresql - SQL multiple natural inner joins -
why correctly homecoming order id
of order, customer id
of person made order, , last name
of employee in charge of transaction
select "orderid", "customerid", "lastname" orders o natural inner bring together customers bring together employees on o."employeeid" = employees."employeeid";
while
select "orderid", "customerid", "lastname" orders o natural inner bring together customers natural inner bring together employees;
returns 0 rows?
i sure have mutual columns.
table orders orderid employeeid customerid ...
table employees employeeid ...
table customers customerid ...
without seeing full, unedited schema it's hard sure, i'd there more mutual columns intended.
e.g. @clockworkmuse sugested:
create table orders ( orderid integer primary key, employeeid integer not null, customerid integer not null, created_at timestamp not null default current_timestamp, ... ); create table employees ( employeeid integer primary key, created_at timestamp not null default current_timestamp, ... );
then orders natural bring together employees
equivalent orders inner bring together employees using (employeeid, created_at)
. certainly isn't intended.
you should utilize inner bring together ... using (colname)
or inner bring together ... on (condition)
.
natural join
poorly thought out feature should avoided except on quick , dirty ad-hoc queries, if then. if works now, if later add together unrelated column table might alter meaning of existing queries. that's ... well, avoid natural joins.
sql postgresql join natural-join
No comments:
Post a Comment