如何检查Join是否存在?

时间:2016-04-28 09:18:38

标签: mysql

我有查询,当条件为真时我添加连接,当我需要检查该连接或其现有的别名时,我还有其他条件。

c2 IS NOT NULL // doesn't work

CASE WHEN c2 IS NOT NULL THEN c2.cost ELSE c.cost END AS cost_opt

LEFT JOIN
costs AS c ON (
  1 = 1
  AND c.id = m.cost_id
)

${entity == 'client' ?
                `
                LEFT JOIN
                  costs AS c2 ON (
                    1 = 1
                    AND c2.provider_id = over.provider_id
                    AND c2.active      IS TRUE
                  )
                `
              : ``
 }

请帮助

1 个答案:

答案 0 :(得分:0)

您无法测试表是否存在,只涉及一列。在您的情况下,因为它是左连接:

WHERE c2.provider_id IS NOT NULL

如果你不想要任何不匹配,你应该RIGHT JOIN排除它们。