多个表不填充Null

时间:2013-04-23 04:20:57

标签: mysql

我有4张桌子:

表1: batch_info with columns batch_id, brew_date, beer_style_name

表2: brew_fermentation with columns batch_id (FK of batch_info), fermentor_name (FK of fermentor),.

表3: fermentor with columns, fermentor_name, tank_volume

表4: Produced_amount with columns batch_id(FK of batch_info), produced_amount, date_transferred

当酿造啤酒时,我们将其输入表1和表2中。当啤酒完成发酵时,我们将数据输入表4 produce_amount。

我想创建一个看起来像这样的表

Fermentor # |  Batch_ID  |  Tank_Size  |  Date_Brewed  | Beer Style

F10.1           13001         10           2013-01-12      IPA

F20.2           NULL         20             NULL          EMPTY

我有以下SQL语句。

然而,它不会显示发酵罐表中的所有发酵罐,只显示目前含有其中某些物质的发酵罐。我想像上面那样填充表格。

SELECT brew_fermentation.fermentor_name AS "Fermentor #",
batch_info.batch_id AS "Batch Number", batch_info.beer_style_name AS
"Beer Style", batch_info.date_brewed AS "Date Brewed",
fermentor.tank_volume AS "BBLs"

FROM batch_info, fermentor, brew_fermentation

WHERE batch_info.batch_id = brew_fermentation.batch_id AND
brew_fermentation.fermentor_name=fermentor.fermentor_name AND

batch_info.batch_id NOT IN (SELECT produced_amount.batch_id FROM
produced_amount)

ORDER BY batch_info.date_brewed

1 个答案:

答案 0 :(得分:0)

NOT IN替换为NOT EXISTS

  ...
WHERE 
  batch_info.batch_id = brew_fermentation.batch_id 
  AND brew_fermentation.fermentor_name=fermentor.fermentor_name 
  AND NOT EXISTS (
    SELECT 1 FROM produced_amount WHERE batch_id = batch_info.batch_id
  )