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 )
Related
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 used the always true statement e.g. 1 = 1 in case statement of where clause in MYSQL with following syntax:
select * from tablename where
(case when tablefield is not null then
then tablefield = 'value'
else 1 = 1 end)
I want to know how can i use else 1 = 1 (always true statement) in sqlserver/tsql case statement in where clause.
you would not use case you would just write a multi conditional statement. In your case it would look like
Where (tablefield = 'value'
OR tablefield is null)
If you want to use the TSQL CASE function, you could do something like :
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 else 0 end)
else 1 end)
which could be simplified to :
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1
else 0 end)
You can leave out the "else 0" parts, as when no match is found, NULL is returned (which will not be equal to 1). i.e.:
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 end)
else 1 end)
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1 end)
Sorry I just mixed up the query,I was intended to ask for a condition when parameter variable is null instead of tablefield
In that case query may look like this :
select * from tablename where
(case when parameterfield_or_variable is not null then
then tablefield = 'value'
else 1 = 1 end)
or when using parameterfield for value
(case when parameterfield_or_variable is not null then
then tablefield = parameter_field_or_variable
else 1 = 1 end)
The answer as per asked question #KHeaney is right.
However the query in tsql/sqlserver as described in mysql, will be like this :
select * from tablename
where tablefield = #parameterfield
-- incase of comparing input parameter field with tablefield
or #parameter is null
so when #parameterfield will be null it will show all results otherwise it will restrict to only input value.
Thanks
You can try...
select * from tablename
where tablefield = isnull(#parameter, tablefield)
I know T-SQL and MySQL both provide COALESCE, which returns the first non-NULL value provided.
SELECT *
FROM tablename
WHERE (COALESCE(`tablefield`, 'value') = 'value')
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')
I have the following data in my table:
id: 1, name: TOM, visible: 1, field_1: EMPTY, field_2: EMPTY, field_3: EMPTY.
SELECT id, name FROM table
WHERE name <> 'TOM' AND visible = 1
AND field_1 <> '' OR field_2 <> '' OR field_3 <> ''
I want to select only the rows where name is different than 'name1' and at least one of the fields is not empty.
I expected no results for this query but i get: id, TOM like the where name condition is overwritten.
Your problem is precedence: bracket the ORs:
SELECT id, name
FROM table
WHERE name <> 'TOM'
AND visible = 1
AND (field_1 <> '' OR field_2 <> '' OR field_3 <> '')
I suspect, that one of the field is really != '', making the condition valid for "Tom" record. You must group OR conditions, using brackets:
SELECT id, name FROM table
WHERE name <> 'TOM' AND visible = 1
AND (field_1 <> '' OR field_2 <> '' OR field_3 <> '')
The and operator has a higher precedence than or (see the documentation for extra details), so if we take your query:
SELECT id, name FROM table
WHERE name <> 'TOM' AND visible = 1
AND field_1 <> '' OR field_2 <> '' OR field_3 <> ''
And add clarifying parentheses, we'll get:
SELECT id, name FROM table
WHERE (name <> 'TOM' AND visible = 1 AND field_1 <> '')
OR field_2 <> ''
OR field_3 <> ''
Which is not what you wanted. You can solve this by explicitly adding parentheses:
SELECT id, name
FROM table
WHERE name <> 'TOM' AND
visible = 1 AND
(field_1 <> '' OR field_2 <> '' OR field_3 <> '')
You should add ()
SELECT id, name
FROM table
WHERE name <> 'TOM'
AND visible = 1
AND (field_1 <> ''OR field_2 <> '' OR field_3 <> '')
Please enclose empty fields comparison in one paranthesis block as show in bold
SELECT id, name FROM table
WHERE name <> 'TOM' AND visible = 1
AND ( field_1 <> '' OR field_2 <> '' OR field_3 <> '' )
You have to insert brackets
Are the empty fields really empty? Maybe the '' doesn't bring the right results
I have created this query:
Where
(companies.col_335 <> NULL) And
(companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes')
I want the selection to only include records where the FIRST comparision above is true, and at least one of the other 5 comparisions are true.
Is this the right way of doing this?
No. AND has higher precedence over OR (basic boolean logic). Use parentheses to do it right:
Where
(companies.col_335 <> NULL) And
((companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes'))
You are missing some brackets:
Where
(companies.col_335 <> NULL) And
(companies.col_285 = '' Or
companies.col_346 <> 'Yes' Or
companies1.col_275 = '' Or
companies1.col_294 <> 'Yes')
What you were trying to do would do the AND FIRST and then do OR with any of the last 3. i e:
(companies.col_335 <> NULL And companies.col_285 = '')
Or
companies.col_346 <> 'Yes'
Or
companies1.col_275 = ''
Or
companies1.col_294 <> 'Yes'
You're almost there... you should surround at paranthesis the or section.
WHERE
(companies.col_335 <> NULL)
AND
(
(companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes')
)