[英]SELECT Statement with IF condition MySQL
我有一張桌子,有幾列
menu_id , parent_id, controller , action , action_edit , action_view ,
select * from test where
( action like 'my_action.php' or
action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
) AND parent_id !=0 ORDER BY `controller` ASC
當action
或action_view
或action_edit
為home.php
>時,我想省略parent_id !=0
。
我嘗試使用
AND IF(action='home.php' OR action_view='home.php' OR action_edit='home.php',parent !=0,)
代替AND parent_id !=0
但這給了我一個令牌不匹配錯誤
但沒有用,請先謝謝你:)
嘗試這樣的事情:
select *
from test
where 'home.php' not in (action, action_view, action_edit) or parent_id <> 0) and
(action like 'my_action.php' or action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
)
編輯:
not in
是危險not in
。 讓我們使用coalesce()
預防問題:
select *
from test
where 'home.php' not in (coalesce(action, ''), coalesce(action_view, ''), coalesce(action_edit, '')) or parent_id <> 0) and
(action like 'my_action.php' or action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
)
也許你想要這個?
select * from test where
(( action like 'my_action.php' or
action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
) AND parent_id !=0)
OR
( action='home.php' OR
action_view='home.php' OR
action_edit='home.php')
ORDER BY `controller` ASC
嘗試這個:
SELECT * FROM test
WHERE NOT EXISTS ((SELECT A.* FROM test A)
JOIN (SELECT B.* FROM test B WHERE
B.action like 'my_action.php'
OR B.action_edit like '%my_action.php%'
OR B.action_view like '%my_action.php%')
ON A.parent_id !=0)
ORDER BY `controller` ASC
嗨最后我有一個解決方案
select * from test where
( action like 'my_action.php' or
action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
) AND parent_id !=0 ORDER BY `controller` ASC
代替parent_id !=0
我用了
和IF(action='home.php' OR action_view='home.php' OR action_edit='home.php',parent=0,parent!=0)
現在它正在工作,謝謝您的幫助:)。 我發布了我的答案,因為我認為這將為以后遇到類似問題的用戶提供幫助:)
怎么樣 :
select * from test where
(( action like 'my_action.php' or
action_edit like '%my_action.php%' or
action_view like '%my_action.php%'
)) AND (parent_id !=0))
ORDER BY `controller` ASC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.