简体   繁体   中英

Determine column data with if condition in a joined tables SQL

I have a query that works well, where i would like an assistance is how to incorporate a condition to determine the value displayed on a given column. That is if a column of a table has a value say "Authorization of COA" then the column date added should show the date else the column should show null

Here is my query so far

SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' , t.date_added (//should show the value of date_added else NULL based on a condition for this column),
    FROM request r
    LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
    LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
    LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
    WHERE r.client_id='$cid'
    AND r.created_at BETWEEN '$start' AND '$end'
    AND a_s.department_id = '$dept'
    GROUP BY r.request_id
    ORDER BY `r`.`created_at` DESC "

A simple CASE statement should be sufficient in this case, I believe.

SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, 
CONCAT(u.fname,' ',u.lname) 'analyst', 
CASE WHEN ConditionColumn = 'Authorization of COA' THEN t.date_added 
ELSE NULL END AS 'date_added' 
    FROM request r
    LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
    LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
    LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
    WHERE r.client_id='$cid'

Give this a try:

SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' , 
IFF(nameOfColumnToTest = 'Authorization of COA', t.date_added, null) AS DateAdded 
    FROM request r
    LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
    LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
    LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
    WHERE r.client_id='$cid'
    AND r.created_at BETWEEN '$start' AND '$end'
    AND a_s.department_id = '$dept'
    GROUP BY r.request_id
    ORDER BY `r`.`created_at` DESC "

In both mysql and ms sql you can use case expression ( mysql case ; ms sql case ) to assign value to a field conditionally:

SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' , 
    CASE WHEN Column_To_Test = 'Authorization of COA' THEN t.date_added
        ELSE null
    END AS date_added 
    FROM request r
    LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
    LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
    LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
    WHERE r.client_id='$cid'
    AND r.created_at BETWEEN '$start' AND '$end'
    AND a_s.department_id = '$dept'
    GROUP BY r.request_id
    ORDER BY `r`.`created_at` DESC "

This way the solution is more portable between the databases. Mysql offers if() function, ms sql iif() to achieve the above, but those are product specific solutions.

Try to use the Case statements like.

CASE WHEN ( Condition_1 AND Condition_2 OR ... Condition_N ) THEN (Value_1) ELSE (Value_1) END

Here Condition _1 can be column record value and its combination of Column record values and Value _1 can be single value [Ex: Null or "some string"] or Returned value from nested sql statement like [Ex: (select count(*) from ...)]

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