sqlite 我想找到所有的记录,即使还没有popoulated与外键

7tofc5zh  于 2023-10-23  发布在  SQLite
关注(0)|答案(1)|浏览(145)

stock

stkid (pk), name

share

price, quantity, stkid (fk)

我运行这个查询,但它只显示那些已经有股票记录的股票。我想显示所有股票,即使没有股票记录。

select name,
       0,
       sum(price*quantity) / sum(quantity) as avg,
       sum(quantity) as qty
from stock,
     share
where share.stkid = stock.stkid
group by (stock.stkid)
kse8i1jr

kse8i1jr1#

您可以使用LEFT JOIN语句,即使share表中没有链接行,它仍然会选择stock表中的行。

SELECT
  name,
  0,
  sum(price*quantity) / sum(quantity) as avg,
  sum(quantity) as qty
FROM stock
LEFT JOIN share ON share.stkid = stock.stkid
GROUP BY stock.stkid

相关问题