简体   繁体   中英

Using multiple conditions within case statement in MYSQL

My aim is to join the users and products tables on the Product_ID. I have attached the users table and products table for your reference below.

users table: Users table

在此处输入图片说明

products table: Products table

在此处输入图片说明

When product_ID is 7 , I need to look into the application date in the users table and compare if it falls within the first_day_active and last_day_active in the products table to get the Commission rate. I used the below query and it doesn't seem to be working. Can someone please help? Thanks!

CASE

WHEN (users.product_ID = 7 AND users.application_date BETWEEN ('2017-04-01','2017-06-01')) THEN products.commission
WHEN (users.product_ID = 7 AND users.application_date BETWEEN ('2017-06-02','2017-09-01')) THEN products.commission
WHEN users.product_ID = 7 AND users.application_date > '2017-09-02' THEN products.commission
ELSE "Something Fishy" END AS TESTING

Schematically:

SELECT Users.Name, 
       Products.Product_name,
       Products.Comission
FROM Users
JOIN Products ON Users.Application_id = Products.Application_id
             AND Users.Application_Date BETWEEN Products.First_day
                                            AND COALESCE(Products.Last_day, Users.Application_Date)
WHERE products.Active = 0

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