2016-02-29 45 views
0

,如:在MySQL中是有可能寫出加入取決於如果條件

SELECT * FROM (`expenditures`) 
JOIN `categories` 
ON `categories`.`cat_id` = `expenditures`.`cat_id` IF(`projects`.`project_id` ! = 0) 
JOIN `projects` 
ON `projects`.`project_id` = `expenditures`.`project_id` 
WHERE `expenditure_id` = $expenditure_id 
+1

的可能的複製[如何使用IF語句一個MySQL連接查詢?](http://stackoverflow.com/questions/10064183/how-do-i-use-an-if-statement-in-an-mysql-join-query) –

+0

不,這是不可能的。你究竟想要完成什麼? – shmosel

回答

0

你可以試試這個:

SELECT * FROM (`expenditures`) 
JOIN `categories` 
ON `categories`.`cat_id` = `expenditures`.`cat_id` 
JOIN `projects` 
ON `projects`.`project_id` = `expenditures`.`project_id` and `projects`.`project_id` ! = 0 
WHERE `expenditure_id` = $expenditure_id 
0
SELECT * FROM (`expenditures`) 
JOIN `categories` 
ON `categories`.`cat_id` = `expenditures`.`cat_id` 
JOIN `projects` 
ON `projects`.`project_id` = `expenditures`.`project_id` and `projects`.`project_id` <> 0 
WHERE `expenditure_id` = $expenditure_id