Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL内部联接不返回具有空值的记录_Sql_Postgresql - Fatal编程技术网

SQL内部联接不返回具有空值的记录

SQL内部联接不返回具有空值的记录,sql,postgresql,Sql,Postgresql,我的“items”表(postgreSQL数据库)中有两种不同类型的记录。某些项目具有关联的invoiceid,该ID具有关联的客户信息。“我的项目”表中的其他项目没有关联的发票号 我正在尝试返回带有发票日期和客户名称的项目列表。未关联发票或客户的项目也将显示,但这些字段将为空。问题在于我当前的sql语句。它仅显示与发票信息关联的项目 select items.ItemID, items.qty, items.description, customers.firstname, customers

我的“items”表(postgreSQL数据库)中有两种不同类型的记录。某些项目具有关联的invoiceid,该ID具有关联的客户信息。“我的项目”表中的其他项目没有关联的发票号

我正在尝试返回带有发票日期和客户名称的项目列表。未关联发票或客户的项目也将显示,但这些字段将为空。问题在于我当前的sql语句。它仅显示与发票信息关联的项目

select items.ItemID, items.qty, items.description, customers.firstname,
customers.lastname, invoices.InvoiceDate, items.status 
from items 
inner join Invoices on items.InvoiceID = Invoices.InvoiceID 
inner join customers on Invoices.CustomerID = Customers.CustomerID 
where Items.Status = 'ONTIME' 
ORDER BY InvoiceDate asc

有没有办法让我的所有记录都显示出来,或者甚至有可能?没有数据的字段是空的,我不确定这是否是问题的一部分

您想使用
左侧外部联接
而不是
内部联接

select i.ItemID, i.qty, i.description, c.firstname,
       c.lastname, inv.InvoiceDate, i.status 
from items i left outer join
     Invoices inv
     on i.InvoiceID = inv.InvoiceID left outer join
     customers c
     on inv.CustomerID = c.CustomerID 
where i.Status = 'ONTIME' 
order by InvoiceDate asc;
我还引入了表别名,使查询更易于阅读