Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6334603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:41:29+00:00 2026-05-24T18:41:29+00:00

I m working with mysql view and i want use IF ELSE statement on

  • 0

I m working with mysql view and i want use IF ELSE statement on that view. its giving me error like this

#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 

This is my view

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

)  

EDITED
here my function

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

If any one can help me thanks

UPDATED query

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 error 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

Done

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:41:30+00:00Added an answer on May 24, 2026 at 6:41 pm

    As Conspicuous commented, a View can hold a simple SELECT statement.

    You can use a single query with CASE block:

    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
    

    Or use a join based query, (creating a 2nd view to workaround the MySql limitation):

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a MySQL query that writes into an outfile. I run this
This is the piece of script that I'm working with; I'm trying to use
Any idea why the following code is not working mysql credentials are correct, have
I've developed an application at working using MySQL 5, that uses Views to access
I'm working with a MySQL database that has some data imported from Excel .
I am working on a MySQL database that is huge (about 120 tables). I
How do you use a php variable (array) inside a mysql select statement? I
I'm working on a mysql query in a Drupal database that pulls together users
I have 2 files, one is used to view the data in the mysql
I have a working timecard in MySQL , but I have no way of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.