mysqlselect-query

How to check multiple condition for same field in my SQL?


Following table:

ECNO    BRANCH   MONTH  ISDELETED
1   B1  February 2018   n
2   B3  February 2018   n
3   B3  March    2018   n
4   B1  March    2018   n
5   B2  January  2018   n
6   B3  January  2018   n

Here we need to select according to branch and starting date and end date.

Case1: If branch is B3, starting date and end date is equal then pick according the branch selected.

select * from HR_BULKBILL_DETAILS where BRANCH='B3' AND MONTH='March 2018'

This query works fine

Case 2: If Staring date (Assume January 2018) and End date (Assume Febuary 2018) And Branch b3. How to check this condition in select query

How Check two condition for one field?

Note: (In database we have only column Month:this hold month- year example : Januray 2018) I am using my sql.

We have tried:

select * from HR_BULKBILL_DETAILS where  MONTH='February 2018' AND MONTH='March 2018' AND BRANCH='CBE3'

Forgive me if any typo.

My Expected Output is:

ECNO BRANCH MONTH ISDELETED 2 B3 February 2018 n 3 B3 March 2018 n

My Input is user give startedate and end date and brach

Startdate and enddate will be like March 2018


Solution

  • If you want to get rows for Feb or March, then say that:

    select  *
        from  HR_BULKBILL_DETAILS
        where  ( MONTH='February 2018' OR  MONTH='March 2018' )
          AND  BRANCH='CBE3'
    

    Be sure to use parentheses, else the precedence of AND and OR will mess up the query.

    If you need between Feb and June, then you cannot do that easily. You would be better off storing a "2018-02-01" in aDATE` column so that you can do comparisons such as

    WHERE month BETWEEN "2018-02-01" AND "2018-06-01"
    

    You can then use a date function (see the manual) to turn "2018-02-01" into "February 2018" in the SELECT statement.