简体   繁体   中英

MYSQL - Multiple Conditions in One Query

I have a table that stores specific training criteria that is required for employees that meet a specific criteria. Their employee profile has a variety of information but the training "profiles" are largely driven by jobtitle, department, province/state, country, and project they are on.

I am trying to determine how to do the following:

  • Find the first column that isn't null (will never be a situation where all columns are null)
  • Check based on the values passed to it from the user profile
  • If this condition is met, move to the next, remaining columns and see if the any other values match.
  • If there is one match and the rest are null, return the row. If more than one column are not null then each passed value must match.

Sample table:

+---------------------------------------------------------------------------------------------+
|                                      trainingProfile_tb                                     |
+---------------------------------------------------------------------------------------------+
| No | course_name | project_id | provinceState_id | country_id | department_id | jobTitle_id |
+----+-------------+------------+------------------+------------+---------------+-------------+
| 1  | Shooting    |       null |                1 |          2 |          null | null        |
| 2  | Running     |          1 |             null |       null |          null | null        |
| 3  | Shooting    |          2 |                3 |       null |             1 | null        |
+----+-------------+------------+------------------+------------+---------------+-------------+

Let's say the employee has a project of 1, province/state of 1, country of 2, department of 1, and job title of 1.

I would want to return courses of shooting and running since what values that are in the table match the non-null values.

I hope this makes sense, but if not please let me know what needs to be clarified and I will do my best. Thanks in advance!

UPDATE

So this seems to have achieved the desired result. Is there a more efficient and/or elegant way of writing this?

SELECT course_name FROM `requiredtraining_tb` WHERE 
CASE WHEN company_id is null THEN TRUE WHEN company_id is not null and company_id=1 THEN TRUE END
AND
CASE WHEN provinceState_id is null THEN TRUE WHEN provinceState_id is not null and provinceState_id=3 THEN TRUE END
AND
CASE WHEN jobTitle_id is null THEN TRUE WHEN jobTitle_id is not null and jobTitle_id=3 THEN TRUE END 

I guess you could start with this...

Select No entity
    , 'course_name'  attribute
    , course_name value
From training_profile_tb
Union 
Select no
     , 'project_id'
     , project_id...

case when key word usually used in select part. If you want fetch some data that has multiple condition and conflict, you can make some temp table like union join between more destributed select . After that you have a table that all need data in there and make the your share conditions. If you want the query!? Please describe more. Regards a.ayati

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM