I am having three parameters $page, $date, $search. i need a single select sql query where i have to check all the three variables one by one whether any of these variables is having some value or not or if all of the three variables is having some value. According to which it will match from the database and give the result. Please help me out.
You can make where clause as follows in your Select query.(Below is MS SQL format, you can modify CASE statement equivalent with MY SQL)
SELECT * FROM TABLE (if more than one table, you can use join)
WHERE TABLE.search_COLUMN = case when #search <> '' then #search else TABLE.search_COLUMN end
AND
TABLE.page_value_COLUMN = case when #page_value <> '' then #page_value else TABLE.page_value_COLUMN end
AND
TABLE.date_value_COLUMN = case when #date_value <> '' then #date_value else TABLE.date_value_COLUMN end
Related
I want to write a query that will verify 8 parameters of a row are the same as the parameters passed to the procedure. If any one of them is different then modify a status, otherwise do nothing.
Is there a way I can check all those parameters in a single IF case? For example:
IF (v_duty <> duty) OR (v_current <> current) OR (v_frequency <> frequency) THEN
* UPDATE ......;
END IF
Or do I have to use an ELSE IF for each comparison I want to make?
The above doesn't work, with or without brackets between each test.
You could probably manage this with a simple UPDATE and WHERE condition:
UPDATE table_name
SET status_column = 'new_status'
WHERE identifying_column = :identifier
AND (
v_duty != :v_duty
OR v_current != :current
OR v_frequency != :frequency
)
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.
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.
I have 3 variables which will be conditional when #path is not NULL i.e
#serverName,#analysisDB,#cubeName. Hence I used a CASE statement in the above SELECT statement. Similarly, for #path is not NULL the #cubename also needs to be conditional for LVL5.L5_CubeName (see REPLACE statement) but its inside a REPLACE statement and the CASE block becomes too complicated for the same. How do I write it if I want to do something like
CASE WHEN #path!='' THEN #cubeName ELSE LVL5.L5_CubeName in the REPLACE statement
SELECT LVL5.L5_Id,
CASE WHEN #path!='' THEN #serverName ELSE LVL5.L5_ServerName END [AnalysisServer],
CASE WHEN #path!='' THEN #analysisDB ELSE LVL5.L5_AnalysisDatabase END [AnalysisDatabase],
REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'##Level1',ISNULL(#lvl1DataVal,'')),
'##Level2',ISNULL(#lvl2DataVal,'')),'##CubeName',ISNULL(#cubeName,'')) [MDXQuery],
LVL5.L5_ReplaceMDX [ReplaceMDX],
LVL5.L5_RefreshDate [RefreshDate],
LVL5.L5_ReportAttribute [ReportAttribute],
LVL5.L5_ReportTitle [ReportTitle]
FROM Report_SR_Level5 [LVL5]
WHERE L4_ID = #L4_ID ORDER BY LVL5.L5_DisplayOrder
Something like this:
CASE WHEN #path is not null or #path != ''
THEN REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'##Level1',
ISNULL(#lvl1DataVal,'')),'##Level2',ISNULL(#lvl2DataVal,'')),'##CubeName',
ISNULL(#cubeName,'')) [MDXQuery]
ELSE Somethingelse END cubedata
What I have is a bit column NonMileage and based on that bit column i want to make a variable that I can use inside of a where clause.
this is a two part question:
How do you case a variable? The code below does not case the
#NoMileageListing
And then I have it setting #MileListingClause as a string, can I
just use #MileListingClause like where #MileListingClause?
.
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT
#NoMileageListing CASE #NoMileageListing when 1 then
SET #MileListingClause = 'tblcargarage.miles >= 0' else
SET #MileListingClause = 'tblcargarage.miles != 0' end case;
here's the answer
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT CASE #NoMileageListing
WHEN 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
found here:
Mysql Storing a variable with the result of an SELECT CASE
I think the problem is located in the when part.
It should be:
SELECT CASE #NoMileageListing
WHEN #NoMileageListing = 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
to create the dynamic query with the #noMileWhereClause refer to the answer here:
How To have Dynamic SQL in MySQL Stored Procedure