我正在尝试创建3个表的视图。有一条线正在弄乱一切,我无法做到正确。它在下面的“IF”声明中。我知道我不能在if语句中使用WHERE,但这正是我想要实现的。
表'c'(qms_dpus
表)是一个存储每个作业的错误的表。我想做的就是说
“对于表'a'中的每个作业,检查表'c'以查看JOBNUM列是否匹配且IS_COMPLETE列是否为0.如果是,则在此新列{{1}中加1如果为false,则在“
列中添加0OPEN_DPUS
当我删除IF语句(以及对表'c'的引用)时,代码工作得很好,除了列CREATE OR REPLACE VIEW `view_qms_linea` AS SELECT
a.ID AS `ID`,
a.JID AS `JID`,
b.HPL AS `HPL`,
b.LINE AS `LINE`,
a.PAREA AS `PRODAREA`,
a.JOBNUM AS `JOBNUM`,
b.MODELNUMBER AS `MODELNUMBER`,
b.CUSTOMER AS `CUSTOMER`,
b.PM_AE AS `PM_AE`,
b.PM_PE AS `PM_PE`,
b.PM_DE AS `PM_DE`,
b.PM_EE AS `PM_EE`,
b.PM_ED AS `PM_ED`,
b.PM_CE AS `PM_CE`,
IF(c.IS_COMPLETE=0 WHERE c.JOBNUM LIKE a.JOBNUM, 1, 0) AS `OPEN_DPUS`
FROM `gen_jobs_in_production` a, `gen_jobs_table` b, `qms_dpus` c
WHERE
a.JOBNUM LIKE b.WO
AND
b.LINE = 1
AND
a.PAREA != 4
AND
a.PAREA <= 7;
之外我还需要我,但是如果我用这个替换IF语句:
OPEN_DPUS
我的视图为我提供了所有数据,但不是12条记录(当前),它重复每条记录12次并无限重复该模式。
我很乐意展示我的桌面结构,但我有很多专栏,只会让这更令人困惑......
非常感谢任何帮助!
答案 0 :(得分:2)
您的查询缺少join
条件。因此,它会在您的查询中返回3个表的交叉连接结果。请尝试以下查询。
CREATE OR REPLACE VIEW `view_qms_linea` AS SELECT
a.ID AS `ID`,
a.JID AS `JID`,
b.HPL AS `HPL`,
b.LINE AS `LINE`,
a.PAREA AS `PRODAREA`,
a.JOBNUM AS `JOBNUM`,
b.MODELNUMBER AS `MODELNUMBER`,
b.CUSTOMER AS `CUSTOMER`,
b.PM_AE AS `PM_AE`,
b.PM_PE AS `PM_PE`,
b.PM_DE AS `PM_DE`,
b.PM_EE AS `PM_EE`,
b.PM_ED AS `PM_ED`,
b.PM_CE AS `PM_CE`,
--change from here
IF(c.IS_COMPLETE = 0, 1, 0) AS `OPEN_DPUS`
FROM `gen_jobs_in_production` a
JOIN `gen_jobs_table` b ON a.JOBNUM = b.WO
JOIN `qms_dpus` c ON c.jobnum = a.jobnum
WHERE b.LINE = 1
AND a.PAREA != 4
AND a.PAREA <= 7;