2011-08-19 95 views
5

我使用mysql視圖,我想在該視圖上使用IF ELSE語句。這樣mysql view if else problem

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if(getUser()="") THEN] 
     select hie_code_1 from hs_hr_emp_level L,hs_hr_u' at line 7 

該公司給我的錯誤是我的看法

drop view if exists vw_hs_hr_employee; 

CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee where 
hie_code_1 in 
(
BEGIN 

    if(getUser()="") THEN 
     select hie_code_1 from hs_hr_emp_level L 
    ELSE 
      select hie_code_1 from hs_hr_emp_level L,hs_hr_users U 
      where L.emp_number=U.emp_number 
       and L.emp_number=getUser() 
       and (U.def_level=1 or U.def_level=4) 
    END if 

) 

此編輯 我的功能

CREATE FUNCTION `getUser`() RETURNS char(50) CHARSET latin1 
RETURN @user 

如果任何人能幫助我感謝

已更新查詢

CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee where 
CASE getUser() 
     WHEN '' 
    THEN 
    select hie_code_1 from hs_hr_emp_level L 
    END 
hie_code_1 in (select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and (U.def_level=1 or U.def_level=4) ) 
or 
hie_code_3 in (select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=2 ) 
or 
    hie_code_4 in (select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=3) 

givinign錯誤syntax to use near 'select hie_code_1 from hs_hr_emp_level L END hie_code_1 in (select hie_code_' at line 6

完成

drop view if exists vw_hs_hr_employee; 
CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee e where CASE WHEN getUser()='' 
    THEN 
    e.emp_number is not null 
    ELSE 
hie_code_1 in (select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and (U.def_level=1 or U.def_level=4) ) 
or 
hie_code_3 in (select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=2 ) 
or 
    hie_code_4 in (select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=3) 

end 
+0

是'的getUser( )'應該是[CURRENT_USER()或USER()](http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user)? –

+0

問題已更新 –

+2

另外,視圖應該是普通的SELECT語句,並且這些視圖不允許像存儲過程那樣進行塊流控制。因此,您需要使用[IF()或CASE ... WHEN..END](http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html) –

回答

3

正如引人注目的評論,一個視圖可以容納一個簡單的SELECT語句。

您可以使用一個單一的查詢與CASE塊:

CREATE VIEW vw_hs_hr_employee as 
SELECT * 
FROM hs_hr_employee 
WHERE CASE WHEN getUser() = '' 
    THEN hie_code_1 IN (
     SELECT hie_code_1 
     FROM hs_hr_emp_level) 
    ELSE hie_code_1 IN (
     SELECT hie_code_1 
     FROM hs_hr_emp_level L,hs_hr_users U 
      WHERE L.emp_number=U.emp_number 
       AND L.emp_number=getUser() 
       AND (U.def_level=1 or U.def_level=4)) 
    END 

或者使用連接基於查詢,(創建第二個觀點要解決MySQL的限制):

DROP VIEW IF EXISTS vw_hs_hr_employee_sub; 

CREATE VIEW vw_hs_hr_employee_sub AS 
SELECT hie_code_1 
FROM hs_hr_emp_level L 
    LEFT JOIN hs_hr_users U 
     ON L.emp_number = U.emp_number 
     AND L.emp_number = getUser() 
     AND (U.def_level=1 or U.def_level=4) 
WHERE getUser() = '' OR U.emp_number IS NOT NULL 
GROUP BY 1; 


drop view if exists vw_hs_hr_employee; 

CREATE VIEW vw_hs_hr_employee as 
SELECT e.* 
FROM hs_hr_employee e JOIN vw_hs_hr_employee_sub USING(hie_code_1) 
+0

錯誤說視圖的SELECT在FROM子句中包含子查詢 –

+0

@roshan更新時與解決方法 –

+0

感謝您的答案仍有問題我更新了問題請參閱 –

0

使用單引號!

... 
if(getUser()='') THEN 
... 
+0

不行::(..... –

+2

@Bohemian:MySQL [允許聲明字符串常量使用單引號或雙引號](http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html),除非你明確地啓用了只允許單引號的選項 –