using if statement in mysql query - mysql

I would like to use if statement in sql query :
what I want :
if(tractions_delivery.send_date_id !=0 ){
date_send_commodities.id = tractions_delivery.send_date_id
}
my query :
from
tractions_delivery,user_address,province,city,date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id
AND
tractions_delivery.address_id = user_address.id
AND
user_address.province_id = province.id
AND
user_address.city_id = city.id
AND
//not work
(tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id = tractions_delivery.send_date_id)
AND
users.id = user_address.user_id

You could use the CASE-statement
SELECT
*
FROM
tractions_delivery,
user_address,
province,
city,
date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id AND
tractions_delivery.address_id = user_address.id AND
user_address.province_id = province.id AND
user_address.city_id = city.id AND
CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
users.id = user_address.user_id

You can only use if statements in stored procedures or functions. If you just write a sql statement unfortunately you cannot use if statements around the query. But you can use logic in the query itself, e.g.:
SELECT CASE WHEN col1 = col2 THEN'col1 equals col2' else 'col1 doesnt equal col2' ELSE
FROM table1
So around doesnt work, but in the field list you can create CASE WHEN ELSE END logic.

CASE or IF() operators can be of help.
Examples,
SELECT (CASE 1 WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE 'More' END) 'Result';
OR
SELECT IF(1=1, 'One', 'Two') 'Result';
These CASE and IF() operators can be used in the SELECT clause to conditionally interpret column values and return in the resultset.
Note: Do not confuse CASE operator here with 'CASE conditional syntax block' that ends with END CASE.

Related

Using case statement or IF inside a WHERE clause

I have several stored procedures that are almost identical but have some different AND parts inside a WHERE clause.
Based on a variable deptname, I want to add additional AND/OR conditions to my already existing WHERE clause. So kind of like IF/CASE WHEN on the part that is different.
Think about it as string concatenation
query_string = 'WHERE a= XYZ AND B= 123"
if deptname = a: query_string + "AND additional conditions for dept a"
else if deptname = b:query_string + "AND additional conditions for dept b"
What is the appropriate way to use a variable?
here is some pseudo code of what I am trying to do
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
--shared parts
personcode = 'C'
AND
persondeptcode = 'MAJ'
--- NOW the different part
IF #deptname = "deptA"
AND
(
PROGRAM_LDESCR IN
(
'prog1',
'prog2',
'prog3'
)
OR
aprogram IN ('aprogram1')
OR
(aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))
);
--- THIS IS A DIFFERENT DEPT SO WE HAVE DIFFERENT AND PART
ELSE IF #deptname = "deptB"
(
PROGRAM_LDESCR IN
(
'1234'
)
OR
aprogram IN ('a1234')
);
You can use a CASE expression in this case, the important thing is to make sure you have an ELSE clause to ensure the expression remains true if #deptname is not one of the two values with extra conditions:
WHERE personcode = 'C'
AND persondeptcode = 'MAJ'
AND (CASE #deptname
WHEN "deptA" THEN PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3')
OR aprogram IN ('aprogram1')
OR aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123')
WHEN "deptB" THEN PROGRAM_LDESCR IN ('1234')
OR aprogram IN ('a1234')
ELSE 1
END)
Here is a simple demo of a CASE expression used in this fashion.
You seem to want something like:
AND
(#deptname = 'dept123' AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234')) OR
#deptname <> 'dept123'
)
To combine the last part of the WHERE clause (if I'm understanding your commented-code correctly), you could do something like the following:
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
personcode = 'C'
AND persondeptcode = 'MAJ'
AND (
(#deptname="deptA" AND (PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3') OR aprogram IN ('aprogram1') OR (aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))))
OR
(#deptname="deptB" AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234'))
)
Normally you would use the WHERE clause to filter out unnecessary rows of data and a CASE statement if you wanted to actually change the value in the SELECT statement (I rarely see CASE statements outside a SELECT clause, unless it is doing something like a complex sort).

Error in nested case query using SQL

Trying to convert below query into SQL, query works fine on MySQL. Problem seems to be the CASE WHEN area field I get same error.
show Msg 102, Level 15, State 1, Line 44 Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 47 Incorrect syntax near the keyword
'AND'. Msg 156, Level 15, State 1, Line 49 Incorrect syntax near the
keyword 'ELSE'.
WHEN T.[StatusID] = 3
THEN
CASE WHEN (((SELECT COUNT(TA1.[Approver_ID]) FROM [QESTORM].[dbo].[CR_TicketApproval] TA1
INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] CFSR1 ON TA1.[SubRoute_ID] = CFSR1.[ID]
WHERE TA1.[Ticket_ID]= #iTkID AND TA1.Active=1 AND CFSR1.Active=1 AND CFSR1.[Sequence] =(SELECT CFSR2.[Sequence] FROM [QESTORM].[dbo].[CR_Ticket] T2 INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] CFSR2 ON T2.[SubRouteID] = CFSR2.[ID]
WHERE T2.[ID] = #iTkID))<(SELECT COUNT(DISTINCT CFSR1.[ID])FROM [QESTORM].[dbo].[CR_Ticket] AS T1 INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_Route] AS CFR1 ON T1.[FormID] = CFR1.[FormID] INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] AS CFSR1 ON CFR1.[ID] = CFSR1.[RouteID]
WHERE CFR1.[Active] = 1 AND CFSR1.[Active] = 1 AND T1.[ID] = #iTkID AND CFSR1.[Category] = 1 AND CFSR1.[Sequence] = ( SELECT CFSR2.[Sequence] FROM [QESTORM].[dbo].[CR_Ticket] AS T2 INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] AS CFSR2 ON T2.[SubRouteID] = CFSR2.[ID]
WHERE T2.[ID] = #iTkID))))
THEN
CASE WHEN ((SELECT COUNT(1) FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=#iTkID And [Active]=1) = 0)
THEN
--ERROR SHOW HERE => ((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
AND nx.actor=3
AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=#iTkID AND Active=1 )
AND appSameSeq.NTLogin=in_NTLogin
AND nx.actor=3 AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID] = #iTkID AND Active = 1)
ELSE 0
END
I'll toss my hat in the ring.
There may be more than one thing wrong with that SQL statement. What I'll point out is this:
CASE WHEN ((SELECT COUNT(1) FROM [QESTORM].[dbo]. [CR_TicketApproval] WHERE [Ticket_ID]=#iTkID And [Active]=1) = 0)
THEN
--ERROR SHOW HERE => ((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
AND nx.actor=3
AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=#iTkID AND Active=1 )
AND appSameSeq.NTLogin=in_NTLogin
AND nx.actor=3 AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID] = #iTkID AND Active = 1)
ELSE 0
END
Are you trying to evaluate a conditional expression, and return the result as a 1 or 0, as if it were a Boolean expression in a programming language?
That doesn't work in TSQL. This kind of expression evaluation:
SET #value = (1 > 0)
... will produce an error. You can't evaluate a conditional expression: you can only use it in a test, like in a WHERE, HAVING, or WHEN clause.
So, if that's what you're doing, you might do better to wrap your conditional evaluation in yet another CASE statement, like this:
THEN
CASE WHEN {complex conditional statement}
THEN 1
ELSE 0
END
ELSE
0
END
One other thing: this is an extremely complex query statement! I haven't analyzed it enough to see whether it could be simplified, but I'd suggest that you do so, with an eye toward using Common Table Expressions in place of some of your subqueries. This can make the query a lot easier to understand (and debug).
We are missing the complete query but it looks like you are opening too many parentheses.
If you look at the line where your error is shown:
((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
You are opening 2 parentheses but only close one.
That is why you get the error near else because you need to close that second parentheses before you can have an else
So you would need either
((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin))
or
(T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
on that line
It looks like you don't have a 'return value' for your case statement on that specific line.
When you flatten your CASE WHEN statement you have something like this:
CASE WHEN <something> THEN
CASE WHEN <something else> THEN
CASE WHEN <something else again> THEN
-- THEN WHAT ?
ELSE
0
END
END
END
Instead of putting a value on the --THEN WHAT spot, you put another conditional statement. You have to select a value here.

SELECT STATEMENT IN A CASE STEMENT INSIDE A SELECT STATEMENT

I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.

mysql error: sub-query returns more than 1 row

I am using two tables here projections_report p and access_rights a. I can't find out why I am getting the error:
subquery returns more than one row
(case when paramChannel='AllC' then p.gl_sal_chan in
(case when dc_lob=0 then (select distinct pr.gl_sal_chan from
projections_report pr) else (select distinct pr1.gl_sal_chan
from projections_report pr1 where pr1.gl_sal_chan
in (select distinct a.gl_sal_chan from access_rights
a where a.userid= paramUserId)) end)
else p.gl_sal_chan = paramChannel end)
I tried using all and any keywords. Please help.
Thanks in advance.
USE LIMIT in sub queries to return only one record as you are using distinct, it might return more than one record
I tried to do it in another way and got it right. Firstly I changed the statement in else condition of second case statement to
(select distinct gl_sal_chan from access_rights where userid = paramUserid)
as both return the same result(my bad) and secondly I changed the entire condition to
(case when (paramChannel = 'AllC' && dc_lob = 0) then '%' = '%' else
(case when (paramChannel='AllC' && dc_lob != 0) then
gl_sal_chan in (select distinct gl_sal_chan from access_rights where userid = paramUserid)
else gl_sal_chan= paramChannel end)end)
Anyway Thanks #all :)

SQL: How to use SELECT results as function input parameters

Here is my query:
SELECT
CASE
WHEN hbn.users.showDistance = 'T'
THEN hbn.distance(u2.lat, u2.lon, hbn.users.lat, hbn,users,lon)
ELSE 0
END as distance,
hbn.users.id,
hbn.users.username,
From hbn.users,
(select hbn.users.lat, hbn.users.lon from hbn.users where hbn.users.id = '1') AS u2
where hbn.users.Id = '8';
This does not work!
I need to use output of the second select statement as input for distance() function.
It looks like you have commas instead of full-stops in the last parameter to hbn.distance?