I need to write a MySQL statement, but not sure how to write it using case statements.
I would like to write something like this:
SELECT
*
FROM
table
WHERE:
IF sign_off_1 IS NOT NULL AND sign_off_1 IS NOT EQUAL TO 'Director'
sign_off_1_status MUST BE EQUAL TO Complete
IF sign_off_2 IS NOT NULL AND sign_off_2 IS NOT EQUAL TO 'Director'
sign_off_2_status MUST BE EQUAL TO Complete
IF sign_off_3 is IS NOT NULL AND sign_off_3 IS NOT EQUAL TO
'Director' sign_off_3_status MUST BE EQUAL TO Complete
Does anyone know the correct syntax to write this query?
It's not clear what you want to achieve. Does a row need to satisfy all three conditions, or just one of them? Either result can be achieved without using CASE expressions.
If the requirement is to use CASE expressions, and you need all three conditions to be true, you could do something like this:
SELECT t.id
FROM mytable t
WHERE CASE
WHEN t.sign_off_1 <> 'Director' AND t.sign_off_1_status = 'Complete'
THEN 1
ELSE 0
END
+ CASE
WHEN t.sign_off_2 <> 'Director' AND t.sign_off_2_status = 'Complete'
THEN 1
ELSE 0
END
+ CASE
WHEN t.sign_off_3 <> 'Director' AND t.sign_off_3_status = 'Complete'
THEN 1
ELSE 0
END
= 3
If you only need one of the conditions to be true, you could replace = 3 with > 0.
Note that an inequality comparison to a literal is sufficient to guarantee the column is not null. (If the column is NULL, the inequality comparison will return NULL, rather than TRUE.)
Again, the same result could be achieved without using CASE expressions.
You need to use OR and AND operator to simulate if condition in where clause. Try this.
SELECT *
FROM table
WHERE ( sign_off_1 <> 'Director'
AND sign_off_1_status = 'Complete' )
OR ( sign_off_2 <> 'Director'
AND sign_off_2_status = 'Complete' )
OR ( sign_off_3 <> 'Director'
AND sign_off_3_status = 'Complete' )
OR ( sign_off <> 'Director'
AND status <> 'Complete')
Update: am not completely sure about your comment. But this is what i understood.
WHERE (( sign_off_1 <> 'Director'
AND sign_off_1_status = 'Complete' )
OR ( sign_off_2 <> 'Director'
AND sign_off_2_status = 'Complete' )
OR ( sign_off_3 <> 'Director'
AND sign_off_3_status = 'Complete' ))
AND ( sign_off <> 'Director'
AND status <> 'Complete')
Related
I have sql like :
SELECT * FROM leads_notes WHERE content <> '' AND lead_id <> ''
I need add rule if type <> close_task then write user_change_task_status IS NULL
My result sql is:
SELECT * FROM leads_notes WHERE content <> '' AND lead_id <> '' IF(task_type <> 'close_task', 'AND user_change_task_status IS NULL',)
But i get many errors.
Cant understand how can i solve this. Please help, thanks!
Don't use if. Boolean logic is sufficient:
WHERE content <> '' AND
lead_id <> '' AND
( type = 'close_task' or user_change_task is null)
Or:
WHERE content <> '' AND
lead_id <> '' AND
NOT ( type = 'close_task' and user_change_task is not null )
I have in my table one row with a char value. When the value is NULL then a false should be outputted. If the value is not NULL then a true should be outputted.
So when I try to set user_group.tUser to 0 or 1 then I'm getting this error:
Invalid column name 'false'.
Invalid column name 'true'.
SELECT COALESCE((SELECT name
FROM v_company
WHERE companyId = userView.companyId), ' ') AS company,
userView.value AS companyUser,
userView.display AS displayedUser,
CASE
WHEN user_group.tUser IS NULL THEN 0
ELSE 1
END AS userIsMemberOfGroup
FROM v_user userView
LEFT OUTER JOIN cr_user_group user_group
ON ( user_group.group = 'Administrators'
AND user_group.tUser = userView.value )
ORDER BY company ASC,
displayedUser ASC
I think this is the logic you want:
SELECT COALESCE(v.name, ' ') as company,
u.value as companyUser, u.display as displayedUser,
(EXISTS (SELECT 1
FROM cr_user_group ug
WHERE ug.group = 'Administrators' AND
ug.tUser = uv.value
)
) as userIsMemberOfGroup
FROM v_user u LEFT JOIN
v_company c
ON c.companyId = v.companyId
ORDER BY company ASC, displayedUser ASC ;
In general, MySQL is very flexible about going between booleans and numbers, with 0 for false and 1 for true.
You can use MySQL IF function to return 'false' when name IS NULL, else 'true':
SELECT IF(name IS NULL, 'false', 'true')
FROM table;
A simple CASE expression would work here:
SELECT
name,
CASE WHEN name IS NOT NULL THEN true ELSE false END AS name_out
FROM yourTable;
We could also shorten the above a bit using IF:
IF(name IS NOT NULL, true, false)
SELECT
CASE
WHEN name IS NULL THEN 'false'
ELSE 'true'
END
FROM
table1;
I have a MySQL query and this is used to bring up certain events. The event_type and etype will change depending on what someone chooses in the form.
Below is what is added to the query based on a form on the webpage.
and event_type = '54' and etype = 'SP'
the full query is
select tevent.event_name, tevent.event_type, min(e_dates.event_date) as eventdate,
tevent.status, tevent.etype
from (tevent LEFT JOIN event_dates on tevent.eventid=event_dates.eventid)
Where status <> 'delete'
AND YEAR(e_dates.event_date) >= YEAR( CURDATE( ) ) and event_type = '54' and etype = 'SP')
group by tevent.eventid
order by (case when tevent.status = 'closed' and e_dates.event_date >= curdate() then 0 else 1 end),
(case when find_in_set(`status`, 'open,pending,approved') then 0 else 1 end),
e_dates.event_date asc, tevent.eventid ASC
This works perfectly for what I need. I shows all the events that are a certain event types and event category.
However, I want ALL queries to include the following statement
((event_type = '54' and etype = 'SM') or (event_type = '50' and
event_prelim = '2'))
the above statement will add the seminars to all event calendars, but will also show each particular event type based on what the person chooses.
I suppose your WHERE clause could look like this
WHERE status <> 'delete'
AND YEAR(e_dates.event_date) >= YEAR(CURDATE())
AND (
event_type NOT IN ('50','54')
OR event_type IS NULL
OR (event_type = '54' AND etype IN ('SP','SM'))
OR (event_type = '50' AND event_prelim = '2')
)
AND's are evaluated before the OR's.
So when using both AND's and OR's in the criteria, putting the parentheses does matter.
I would like to categorize records based on the count of one field.
My code is :
select `posts`.`post_customer_fs_id` AS `post_customer_fs_id`,count(`post_id`) AS `open_post_count`,
CASE count(`post_id`)
WHEN count(`post_id`)=1 THEN 'A'
WHEN count(`post_id`)>1 THEN 'B'
END AS AAA
from `posts` where (`posts`.`post_status` = '3') AND posts.post_type_id='1' group by `posts`.`post_customer_fs_id` ;
For some reason I m receiving A for the records with COUNT 1 but NULL for the records with COUNT bigger than 1.
The post_id is an INTEGER field..
Any help it will be very appreciated!
In
CASE count(post_id)
WHEN count(post_id)=1 THEN 'A'
You are comparing count(post_id) with count(post_id)=1 where the latter results in true or false which equals 1 or 0 in MySQL. You want
CASE
WHEN count(post_id)=1 THEN 'A'
instead.
TRY :::
select post_customer_fs_id AS 'post_customer_fs_id', count(post_id) AS 'open_post_count',
CASE
WHEN count(post_id)=1 THEN 'A'
WHEN count(post_id)>1 THEN 'B'
ELSE 'C'
END AS 'AAA'
from posts where post_status = '3' AND post_type_id= '1'
group by post_customer_fs_id ;
I'm trying to fetch data from table where I'm using a CASE condition in the WHERE clause and currently I'm using following query:-
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND (
STATUS = 'Sold'
OR STATUS = 'In Stock'
OR STATUS = 'Ref'
)
AND CASE WHEN (
STATUS = 'Sold'
)
THEN delivery_date >= '2012-08-01'
END
But it returns 0 for total and NULL for purchase.
From your comment.
I want to use Case Statement, could u pls clarify me about case statament in where clause
You can use CASE statement in WHERE like this:
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND ( STATUS = 'Sold'
OR STATUS = 'In Stock'
OR STATUS = 'Ref')
AND CASE STATUS
WHEN 'Sold'
THEN delivery_date >= '2012-08-01'
ELSE 1=1
END
Here you need to use ELSE 1=1. otherwise you will not get desired result. For more explanation see this SQLFiddle
I don't think that CASE can work that way. What you want is a slightly more complex expression as your WHERE clause. Probably something like this:
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND (
(STATUS = 'Sold' AND delivery_date >= '2012-08-01')
OR STATUS = 'In Stock'
OR STATUS = 'Ref'
)