MySQL IF AND OR - mysql

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')
)

Related

Sql add condition IF

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 )

SQL Convert a char to boolean

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;

MySQL query case statement in where optimization

I am writing a query in mysql i am not familer with the recursive query to much.
how and what i need to do to optimization of the below query as the conditions i put not looks good ,there must be a easier way to do same.
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
and COALESCE(a.Geography_Country, '') =
case
when
a.Geography_Country is null
then
COALESCE(a.Geography_Country, '')
else
b.Geography_Country
end
and COALESCE(a.Geography_DistrictOrCounty, '') =
case
when
a.Geography_DistrictOrCounty is null
then
COALESCE(a.Geography_DistrictOrCounty, '')
else
b.Geography_DistrictOrCounty
end
and COALESCE(a.Geography_State, '') =
case
when
a.Geography_State is null
then
COALESCE(a.Geography_State, '')
else
b.Geography_State
end
and COALESCE(a.Geography_City, '') =
case
when
a.Geography_City is null
then
COALESCE(a.Geography_City, '')
else
b.Geography_City
end
Intro
I noticed you could rewrite some of these statements in a much simpler form.
So for example:
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
can simply become:
AND ( a.Geography_Geography is null or a.Geography_Geography = b.Geography_Geography )
Final Solution
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
AND ( a.Geography_Geography is null OR a.Geography_Geography = b.Geography_Geography )
AND ( a.Geography_Country is null OR a.Geography_Geography = b.Geography_Country )
AND ( a.Geography_DistrictOrCounty is null OR a.Geography_DistrictOrCounty = b.Geography_DistrictOrCounty )
AND ( a.Geography_State is null OR a.Geography_State = b.Geography_State )
AND ( a.Geography_City is null OR a.Geography_City = b.Geography_City );

not retrieving exact result from case statement

I am using below query but Y column not retrieving exact result. Could you guys please help me out
SELECT FAPI.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI,
NUATON NUMF
WHERE FAPI.BRCH = NUMF.BRCH
AND FAPI.BASE = NUMF.BASE
AND FAPI.NUM = 0
AND NVL(FAPI.RATION, 'X') <> 'D'
AND FAPI.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPI.EFF_DATE
AND FAPI.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI
Thank you,
Rave
Based on #SlimsGohst comment:
You should replace FAPIx with FAPI1 or FAPI2 whichever is applicable
SELECT FAPI1.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI2,
NUATON NUMF
WHERE FAPIx.BRCH = NUMF.BRCH
AND FAPIx.BASE = NUMF.BASE
AND FAPIx.NUM = 0
AND NVL(FAPIx.RATION, 'X') <> 'D'
AND FAPIx.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPIx.EFF_DATE
AND FAPIx.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI1

SQL Conditional Statement in Where Clause

I would like to write the following IF statement in SQL. Most of the where clause is constructed, but it's the condition around the statement that I'm struggling with:
if #StuYear = 11 then
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABU]%' AND Result <> ''
else if #StuYear = 10 then
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABC]%' AND Result <> ''
end if
Simply replace if with where and else if with or and surround the other conditions in paranthesis:
...
where (#StuYear = 11
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'U'
AND Result Not Like '' )
OR (#StuYear = 10
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'C'
AND Result Not Like '')
Tim's answer helped point me in the right direction. However, I eventually resolved the issue by applying the year condition within the where clause as opposed to rapping it around them:
where
(#StuYear = [stuyear]
AND #TeachingGroup = 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')
OR
(#StuYear = [stuyear]
AND #TeachingGroup Not Like 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND [TeachingGroup] = #TeachingGroup
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')