Add where clause argument if variable is set - mysql

I have a WHERE-clause in my SQL-query, and would like to add an argument depending on if the #Fran variable is set.
If #Fran is NULL, then it shouldn't filter the results at all. Otherwise, if #Fran is set, then it should add a WHERE clause "AND franid=#Fran". How could i do that?

SELECT ...
FROM ...
WHERE ...
AND ((#Fran IS NULL) OR (#Fran IS NOT NULL AND franid=#Fran));

AND IFNULL(#Fran, franid) = franid
results in TRUE if #Fran is NULL (then it is essentially franid = franid) and in #Fran = franid if it is not NULL.

Related

MySQL Filter Where if Column= NULL or Value

I am using Python with MYSql string queries and trying to do something like
"SELECT id from Example
WHERE item_column = (%s)"
Where in my case the passed in value can be a number or NULL. I understand that in SQL NULL represents nothing so this equality will not work. Normally, I would have to do something like column IS NULL. However, how do I set up my statement to account for values that can be NULL or not NULL?
Thank you
You can use the NULL-safe equal operator <=>.
"SELECT id from Example WHERE item_column <=> (%s)"
If you want give a default value for your variable incase its null you can use coalese(say 'xx' in case of string and -99.99 in case of integer)
"SELECT id from Example
WHERE item_column = COALESCE((%s),-99.99)"
But if you want to modify your statement based on the whether variable is null or not then you have to do a if else in the code
if variable is None:
return "SELECT id from Example WHERE item_column is null"
else:
return "SELECT id from Example WHERE item_column = variable"

Skipping AND when variable IS NULL

I have an existing Stored Procedure and i add some parameter to filter the result.But when #id_account is null it returns Empty Data. I want to skip AND when #id_account is null, i try IF condition but it wont work. Any suggestion? Thanks :D
AND
(
pd_account.id_account = #id_account
)
Put in the condition you really want, which is:
(pd_account.id_account = #id_account or #id_account is null)
Use:
AND
(
pd_account.id_account = COALESCE(#id_account,pd_account.id_account)
)
From COALESCE doc
Returns the first non-NULL value in the list, or NULL if there are no
non-NULL values.
So when #id_account is not provided the condition will be pd_account.id_account =pd_account.id_account which is equivalent to 1=1 always true.
You can use CASE like:
AND pd_account.id_account = CASE WHEN #id_account IS NOT NULL THEN #id_account
ELSE pd_account.id_account
END

NULL != NULL in mysql query

I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)

Null value matching in mySql

I have three tables in a mysql database . Deseasetype(DTID,TypeName) , Symptom(SID, SymptomName, DTID) , Result(RID, SID1, SID2, SID3, result).1st two table, i think is clear enough.
In result table: there will be combination's of symtoms and any values of SymID1/ SymID2/ SymID3 can be null. here i send a picture of the table result.
I want to input some symptom and output will be the result from the 'Result' table.
For that i wrote this query:
$query = "select Result from result where (result .SID1= '$symptom1') AND (result.SID2= '$symptom2' ) AND (result.SID3 = '$symptom3')";
This work only when three symptom's have value. but if any of the symptom's are null, then no result found. May be the query should be more perfect.
**please avoid any syntax error in my writing.
That's because you are comparing NULL to an empty string, and they aren't equal. You could try this instead:
SELECT Result
FROM symptom
WHERE IFNULL(symptom.SID1, '') = '$symptom1'
AND IFNULL(symptom.SID2, '') = '$symptom2'
AND IFNULL(symptom.SID3, '') = '$symptom3'
Notes:
You need to correctly escape the values of $symptom1, $symptom2 and $symptom3.
This won't efficiently use indexes.
As mark pointed out, the query is eventually falling down to compare with null if you are not escaping the null.
Or you can slightly change your logic to show a empty symptom with value '0' and then using the coalesce function you can easily build your query.
Does this work?
$query = "select Result from result
where (result.SID1 = '$symptom1' OR result.SID1 IS NULL) AND
(result.SID2 = '$symptom2' OR result.SID2 IS NULL) AND
(result.SID3 = '$symptom3' OR result.SID3 IS NULL)";

MySQL Left Join, SELECT a field either from one or the other if null

I am trying to LEFT JOIN 2 tables. which is working out fine. But i am getting back two sets of fields named setting_value. iam trying to get tblSettings.setting_value only if tblAgencySettings.setting_value is NULL. How would i go about doing this? I know i can rename the fields, then in PHP i can check the tblAgencySettings.setting_value and if NULL then grab the tblSettings.setting_value but i prefer to keep this at MySQL.
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
`tblSettings`.`setting_value`, `tblAgencySettings`.`setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
slight issue i just noticed. i failed to mention this. if tblAgencySettings.setting_value does have a value. but changeable is not 1 then just select tblSettings.setting_value
Just add a COALESCE:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
The COALESCE function returns the first non-NULL value you give it so this:
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
Will be tblAgencySettings.setting_value if that's not NULL and tblSettings.setting_value if tblAgencySettings.setting_value is NULL.
If tblAgencySettings.setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above:
COALESCE(
IF(`tblAgencySettings`.`setting_value` = 0, NULL, `tblAgencySettings`.`setting_value`),
`tblSettings`.`setting_value`
)
The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement:
case
when `tblAgencySettings`.`setting_value` = 0 then `tblSettings`.`setting_value`
when `tblAgencySettings`.`setting_value` IS NULL then `tblSettings`.`setting_value`
else `tblSettings`.`setting_value`
end
Change your SQL Statement to this:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
CASE WHEN `tblSettings`.`setting_value` IS NULL THEN `tblAgencySettings`.`setting_value`
ELSE `tblSettings`.`setting_value` END AS `setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
Here's a link to MYSQL CASE Statement for your reference.