sql - 如何在postgresql中连接3个表

标签 sql postgresql join

我有三个表

Item master:存储item master的详细信息

id integer NOT NULL DEFAULT, primary key
name character varying(255),
description character(255),
price double precision,
tax double precision,
readytosales character(1) DEFAULT 'N'::bpchar,
itemgroupid integer,
uom character varying(30),
quantity double precision DEFAULT 0,

purchase:商店购买明细

purchaseid integer NOT NULL DEFAULT,
quantity double precision DEFAULT 0,
purchasemasterid integer NOT NULL,
itemid integer NOT NULL,
itemprice double precision DEFAULT 0.00,

sales:店铺销售明细

salesid integer NOT NULL DEFAULT,
quantity double precision DEFAULT 0,
salesmasterid integer NOT NULL,
itemid integer,
itemprice double 

用于获取股票摘要的公式是

itemmaster.quantity + purchase.quantity -sales.quantity 

我使用下面的查询来获取详细信息,但无法获取结果

select im.id as itemid, 
       name as itemname,
       im.quantity as oepningquantity, 
       im.price as openingprice,
       (im.quantity * im.price) as openingbalance,
       p.quantity as purchasequantity, p.itemprice as purchaseprice,
       (p.quantity * p.itemprice)as totalpurchaseprice, 
       s.quantity as salesquanity, s.itemprice as saleprice,
       (s.quantity *s.itemprice)as totalsalesprice
from item_master as im 
  full outer join purchase as p on im.id=p.itemid 
  full outer join sales as s on im.id=s.itemid 

最佳答案

您的查询有一个小问题。 正确的查询是:

   select im.id as itemid, 
   name as itemname,
   im.quantity as oepningquantity, 
   im.price as openingprice,
   (im.quantity * im.price) as openingbalance,
   p.quantity as purchasequantity, p.itemprice as purchaseprice,
   (p.quantity * p.itemprice)as totalpurchaseprice, 
   s.quantity as salesquanity, s.itemprice as saleprice,
   (s.quantity *s.itemprice)as totalsalesprice

_____from item_master as im ______ ------This Statement is Wrong.

   from item_master -- Try This One.

   full outer join purchase as p on im.id=p.itemid 
   full outer join sales as s on im.id=s.itemid 

关于sql - 如何在postgresql中连接3个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707895/

相关文章:

Sqlite 连接表并选择列包含的位置

php - 获取MYSQL中重复条目的最新数据

Mysql多连接计数

SQL如何让绝对函数正常工作

database - Postgres 的自动调整工具?

java - 使用 IdClass 连接表

sql - 一列上的 Oracle Distinct

mysql - 如何找到不同行单元格之间的最大差异

sql - 在 Postgresql 中使用排名函数的第三高薪水

MYSQL 连接查询建议(删除孤立行)