我有两张桌子。我想根据某些条件离开其中一个表。我想做这样的事情:
IF (Projects.paymentType==1)
LEFT JOIN ProjectBudgetFixedPrice ON (Projects.budget=ProjectBudgetFixedPrice.id)
ELSE
LEFT JOIN ProjectBudgetPerHourPrice ON (Projects.budget=ProjectBudgetPerHourPrice.id)
我创建了以下内容,但它无效。
SELECT * FROM Projects LEFT JOIN
(IF (Projects.paymentType==1,ProjectBudgetFixedPrice,ProjectBudgetPerHourPrice))
ON
(IF (Projects.paymentType==1,Projects.budget=ProjectBudgetFixedPrice.id,Projects.budget=ProjectBudgetFixedPrice.id))
WHERE (Projects.statusCode=3 OR Projects.statusCode=4) AND Projects.suspended='N' AND (Projects.paymentType=1 OR Projects.paymentType=2)
请提供任何帮助或建议?
答案 0 :(得分:2)
您应该可以使用UNION。
像
这样的东西SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetFixedPrice
ON Projects.budget = ProjectBudgetFixedPrice.id
WHERE
Projects.paymentType = 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2))
UNION
SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetPerHourPrice
ON Projects.budget = ProjectBudgetPerHourPrice.id
WHERE
Projects.paymentType <> 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2));
HTH